[go: up one dir, main page]

0% found this document useful (0 votes)
22 views5 pages

Practical 15

This document contains 4 Python code examples demonstrating: 1) Multiple inheritance with a Derived class inheriting from Base1 and Base2 classes. 2) Defining an Employee class with name, salary, department attributes and read/display methods. Two Employee objects are created and their details read and displayed. 3) Defining a Student class with name, college, roll attributes and a Test subclass displaying the student info. 4) Defining Person and Student parent classes and a Resident child class inheriting from both and overriding their constructors. An object is created and its attributes accessed.

Uploaded by

Madhura Kank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Practical 15

This document contains 4 Python code examples demonstrating: 1) Multiple inheritance with a Derived class inheriting from Base1 and Base2 classes. 2) Defining an Employee class with name, salary, department attributes and read/display methods. Two Employee objects are created and their details read and displayed. 3) Defining a Student class with name, college, roll attributes and a Test subclass displaying the student info. 4) Defining Person and Student parent classes and a Resident child class inheriting from both and overriding their constructors. An object is created and its attributes accessed.

Uploaded by

Madhura Kank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name :- Madhura Kank

Roll no :- 23
Practical 15

a)
# Python example to show the working of multiple inheritance
class Base1(object):
def __init__(self):
self.str1 = "Book1"
print("Base1")

class Base2(object):
def __init__(self):
self.str2 = "Book2"
print("Base2")

class Derived(Base1, Base2):


def __init__(self):
# Calling constructors of Base1and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")

def printStrs(self):
print(self.str1, self.str2)

ob = Derived()
ob.printStrs()
2)
class Employee:
'Common base class for all employees'

def __init__(self):
self.name = ""
self.salary = 0.0
self.department= ""

def readEmployee(self):
self.name=input("Enter Name")
self.salary=float(input("Enter salary"))
self.department=input("Enter department")

def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ", self.salary, "Department:",
self.department)
"This would create first object of Employee class"
emp1 = Employee()
"This would create second object of Employee class"
emp2 = Employee()
emp1.readEmployee()
emp2.readEmployee()
emp1.displayEmployee()
emp2.displayEmployee()

3) class Student:
def __init__(self):
self.name = input("Enter your name:")
self.cname = input("Enter your college name:")
self.roll = int(input("Enter your roll number:"))
class Test(Student):
def display(self):
print("============ Student info is ==========")
print("Name is : ", self.name)
print("College Name is:", self.cname)
print("Roll number is:", self.roll)
obj = Test()
obj.display()

4) #definition of the class starts here


class Person:
#defining constructor
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge

#defining class methods


def showName(self):
print(self.name)

def showAge(self):
print(self.age)

#end of class definition

# defining another class


class Student: # Person is the
def __init__(self, studentId):
self.studentId = studentId

def getId(self):
return self.studentId

class Resident(Person, Student): # extends both Person and Student class


def __init__(self, name, age, id):
Person.__init__(self, name, age)
Student.__init__(self, id)

# Create an object of the subclass


resident1 = Resident('Jivan', 30, '102')
resident1.showName()
print(resident1.getId())
resident1.showAge()

You might also like