Inheritance Practice Questions - ( DS140823 ) - 27-09-2023

Create a base class called Person with attributes name and age in constructor . Then, create a derived class called Employee that inherits from Person and adds an additional attribute employee_id in constructor. Create an instance of the Employee class and print out its name, age, and employee ID.

#Create a base class called Person with attributes name and age in constructor . Then, create a derived class called Employee that inherits from Person and adds an additional attribute employee_id in constructor. Create an instance of the Employee class and print out its name, age, and employee ID.
class Person:
  def __init__(self,name,age):
      self.name=name
      self.age=age
  def Person_details(self):
      return "Person name and age:",self.name,self.age
class Employee(Person):
  def __init__(self,name,age,employee_Id):
      self.employee_Id=employee_Id
      Person.__init__(self, name, age)
  def Employee_details(self):
      return "Employee details as:",self.name,self.age,self.employee_Id
e=Employee("mamatha",32,600650)
p=Person("chaithu",25) 
print(p.Person_details())
print(e.Employee_details())
 
output:-
('Person name and age:', 'chaithu', 25)
('Employee details as:', 'mamatha', 32, 600650)
1 Like
class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age
class Employee(Person):
    def __init__(self,name,age,emp_id):
        self.emp_id = emp_id
        Person.__init__(self,name,age)

emp = Employee("Shruti Sharma",29,84606)

print("Employee details: \n")
print("Name:",emp.name)
print("Age:",emp.age)
print("Employee ID:",emp.emp_id)
1 Like
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Employee(Person):
    def __init__(self, name, age, employee_id):
        super().__init__(name, age)
        self.employee_id = employee_id

name = input("Enter the employee's name: ")
age = input("Enter the employee's age: ")
employee_id = input("Enter the employee's ID: ")

employee = Employee(name, age, employee_id)

print("Name:", employee.name)
print("Age:", employee.age)
print("Employee ID:", employee.employee_id)

1 Like
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Employee(Person):
    def __init__(self, name, age, employee_ID):
        super().__init__(name, age)
        self.employee_ID = employee_ID

employee = Employee("Vinay", 22, 12002490)
print("Employee_name : ",employee.name)
print("Employee_age : ",employee.age)
print("Employee_ID : ",employee.employee_ID)
1 Like

class person:
    def __init__(self,name,age):
        self.name =name
        self.age =age
class employee(person):
    def __init__(self, employee_id,name,age):
        super().__init__(name,age)
        self.employee_id =employee_id
obj =employee("id:No.7","name:dhoni","age:42") 
print(obj.name)
print(obj.age)
print(obj.employee_id)

1 Like
# Create a base class called Person with attributes name and age in constructor . 
# Then, create a derived class called Employee that inherits from Person and adds an additional 
# attribute employee_id in constructor. 
# Create an instance of the Employee class and print out its name, age, and employee ID.

class person:
    def __init__(self,name,age):
        self.name = name
        self.age = age

class employee(person):
    def __init__(self, name, age,employee_id):
        super().__init__(name, age)
        self.employee_id = employee_id

employee = employee("Ciby", 30, "C001")

print("Name:", employee.name)
print("Age:", employee.age)
print("Employee ID:", employee.employee_id)

1 Like
class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age
class Employee(Person):
    def __init__(self,name,age,emp_id):
        self.emp_id = emp_id
        super().__init__(name,age)

name = input("Enter the Employee's Name: ")
age = input("Enter the Employee's Age: ")
employee_id = input("Enter the Employee's ID: ")

emp = Employee(name, age, employee_id)


print("Name:",emp.name)
print("Age:",emp.age)
print("Employee ID:",emp.emp_id)

**Note:** mam this code is did from jupyter so I can't change format
1 Like
class person:
    def __init__(self,name,age):
        self.name = name
        self.age = age

class employee(person):
    def __init__(self, name, age,employee_id):
        super().__init__(name, age)
        self.employee_id = employee_id

emp = employee("Martin", 28, "E7876")
print("Name:", emp.name)
print("Age:", emp.age)
print("Employee ID:", emp.employee_id)
class person:
    def __init__(self,name,age):
        self.name = name
        self.age = age

class employee(person):
    def __init__(self, name, age,employee_id):
        super().__init__(name, age)
        self.employee_id = employee_id

employee=employee("pallu",22,22022)
print("Name :",employee.name)
print("age :",employee.age)
print("ID :",employee.employee_id)```