0% found this document useful (0 votes)
13 views

Pract15

The document demonstrates simple inheritance and multiple inheritance in Python through examples. It defines parent and child classes to show inheritance and methods to access attributes and methods of parent and child classes. It also contains exercises with solutions to create classes for employee, student, and multiple inheritance.

Uploaded by

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

Pract15

The document demonstrates simple inheritance and multiple inheritance in Python through examples. It defines parent and child classes to show inheritance and methods to access attributes and methods of parent and child classes. It also contains exercises with solutions to create classes for employee, student, and multiple inheritance.

Uploaded by

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

Subject: PWP[22616]

Practical No.15: demonstrate following operations: Simple inheritance, Multiple inheritance


Name: Ritesh Doibale Roll no:41
Implemetation: Simple Inheritance:
# Parent class created
class Parent:
parentname = ""
childname = ""
def show_parent(self):
print(self.parentname)
# Child class created inherits Parent class
class Child(Parent):
def show_child(self):
print(self.childname)
ch1 = Child() # Object of Child class
ch1.parentname = "Vijay" # Access Parent class attributes
ch1.childname = "Parth"
ch1.show_parent() # Access Parent class method
ch1.show_child() # Access Child class method
Output:

Multiuple Inheritance:

class Add:
def Addition(self,a,b):
return a+b;
class Mul:
def Multiplication(self,a,b):
return a*b;
class Derived(Add,Mul):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Addition(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:

1|Page
Subject: PWP[22616]
Practical No.15: demonstrate following operations: Simple inheritance, Multiple inheritance
Name: Ritesh Doibale Roll no:41
X.Exercise.

Q1. Create a class Employee with data members: name, department and salary. Create suitable
methods for reading and printing employee information

class Employee:

name = ""
department = ""
salary = 0

def read_employee_info(self):
self.name = input("Enter the employee name: ")
self.department = input("Enter the employee department: ")
self.salary = float(input("Enter the employee salary: "))

def print_employee_info(self):
print(f"Name: {self.name}")
print(f"Department: {self.department}")
print(f"Salary: {self.salary}")
emp=Employee()
emp.read_employee_info()
emp.print_employee_info()

Output:

Q2. Python program to read and print students information using two classes using simple inheritance

class Student_Details:
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 Temp(Student_Details):
def display(self):
print("============ Student Details are ==========")
print("Name: ", self.name)
print("College Name:", self.cname)
print("Roll number:", self.roll)

2|Page
Subject: PWP[22616]
Practical No.15: demonstrate following operations: Simple inheritance, Multiple inheritance
Name: Ritesh Doibale Roll no:41

obj1 = Temp()
obj1.display()
Output:

Q3. Write a Python program to implement multiple inheritance

class A:
def __init__(self):
self.var_a = "I am from class A"

def display_a(self):
print(self.var_a)

class B:
def __init__(self):
self.var_b = "I am from class B"

def display_b(self):
print(self.var_b)

class C(A, B):


def __init__(self):
super().__init__()
B.__init__(self)

obj = C()
obj.display_a()
obj.display_b()
Output:

3|Page

You might also like