0% found this document useful (0 votes)
21 views8 pages

PRG6

The document provides examples of different types of inheritance in Python, including single, multiple, multilevel, and hierarchical inheritance, along with code snippets for each type. It also covers operator overloading and abstraction with examples demonstrating how these concepts work in practice. Additionally, a BMI calculation class is included to illustrate practical application of class methods.

Uploaded by

barathkumar9176
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)
21 views8 pages

PRG6

The document provides examples of different types of inheritance in Python, including single, multiple, multilevel, and hierarchical inheritance, along with code snippets for each type. It also covers operator overloading and abstraction with examples demonstrating how these concepts work in practice. Additionally, a BMI calculation class is included to illustrate practical application of class methods.

Uploaded by

barathkumar9176
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/ 8

6) B) Inheritance

1.Single Inheritance

class Student:
def personal(self,sno,sname):
self.sno=sno
self.sname=sname
def Pers_print(self):
print("student no:",self.sno,"student name:",self.sname)
class marks(Student):
def mark_det(self,total,avg):
self.total=total
self.avg=avg
def mark_print(self):
print("total marks:",self.total,"average:",self.avg)
obj=marks()
obj.personal(101,"Rohith")
obj.Pers_print()
obj.mark_det(501,7549)
obj.mark_print

OUTPUT:

Student no: 101 Student Name: Rohith


2. Multiple Inheritance

class person:
def details(self,name,age):
self.name=name
self.age=age
def print1(self):
print("PAssanger name=",self.name,"Passanger age=",self.age)
class journey:
def jou_det(self,date,source,des):
self.date=date
self.source=source
self.des=des
def print2(self):
print("journey date=",self.date,"departure=",self.source,"destination=",self.des)
class ticket(person,journey):
def amt(self,count):
self.count=count
print("ticketfare=",self.count*450)
obj=ticket()
obj.details("rohith",22)
obj.print1()
obj.jou_det("17/10/2022","Chennai","Madurai")
obj.print2()
obj.amt(5)

OUTPUT:

Passanger name=rohith Passanger age=22

Journey date=17/10/2023 departure =Chennai

Destination =madurai
3. Multilevel Inheritance

class Student:
def getStudentInfo(self,sno,sname):
self.sno=sno
self.sname=sname
def printStudentInfo(self):
print("student no:",self.sno,"Name:",self.sname)
class marks(Student):
def getmarks(self,java,python,vb):
self.java=java
self.python=python
self.vb=vb
def printmarks(self):
print("Marks in various sub:",self.java,self.python,self.vb)
def TotalMarks(self):
return(self.java+self.python+self.vb)
class Result(marks):
def getResult(self):
self.total=self.TotalMarks()
def putResult(self):
print("total marks out of 300:",self.total)
S1=Result()
S1.getStudentInfo(101,"Preethi")
S1.getmarks(85,75,65)
S1.TotalMarks()
S1.printStudentInfo()
S1.printmarks()
S1.getResult()
S1.putResult()
S1.getResult()
S1.putResult()

OUTPUT:

Student no:101 Name:Smith

Marks in Various sub: 85 75 65

Total marks out of 300:225


4. Hierarchical Inheritance

class Animals:
def func1(self):
print(“TYPE OF THE ANIMAL.”)
class Domestic(Animals):
def func2(self):
print(“IT IS A DOMESTIC ANIMAL.”)
class Wild(Animals):
def func3(self):
print(“IT IS A WILD ANIMAL”)
Obj1=Domestic()
Obj2=Wild()
Obj1.func1()
Obj1.func2()
Obj2.func1()
Obj2.func3()

OUTPUT:

TYPE OF THE ANIMAL

IT IS A DOMESTIC ANIMAL

TYPE OF THE ANIMAL

IT IS A WILD ANIMAL
6) C) i) Operator Overloading

class A:
def __init__(self,a):
self.a=a
def __add__(self,o):
return self.a + o.a
def __lt__(self,o):
if(self.a < o.a):
return ("01 is less than 02”)
else:
return ("02 is less than 01")
O1=A(5)
O2=A(6)
O3=A ("amjain")
O4=A ("college")
print(O1+O2)
print(O3+O4)
print(O1<O2)

OUTPUT:

11

Amjaincollege

01 is less than 02
ii. Overloading

class complex:
def __init__(self,a,b):
self.a=a
self.b=b
def __add__(self,other):
return self.b+other.b
O1=complex(6,7)
O2=complex(4,3)
O3=O1+O2
print(O3)

OUTPUT:

10
D) Abstraction

from abc import ABC


class polygon(ABC):
def noofside(self):
Pass
class Triangle(polygon):
def noofsides(self):
print("I have 3 sides")
class Pentogon(polygon):
def noofsides(self):
print("I have 5 sides")
class Hexagon(polygon):
def noofsides(self):
print("I have 6 sides")
class Quadrilateral(polygon):
def noofsides(self):
print("I have 4 sides")
R=Triangle()
R.noofsides()
K=Quadrilateral()
K.noofsides()
R=Pentogon()
R.noofsides()
K=Hexagon()
K.noofsides()

OUTPUT

I have 3 sides

I have 4 sides

I have 5 sides

I have 6 sides
6 A) Bmi Calculation

class person:
def __init__(self,name,age,ht,wt):
self.name=name
self.age=age
self.ht=ht
self.wt=wt
def getBMIresult(self):
BMI=(self.wt/(self.ht/100)**2)
if BMI<=15:
print("your underweight.")
elif 15<BMI<=24.9:
print("your weight is normal.")
elif 25<BMI<=29.29:
print("you are overweight.")
else:
print("you are obese.")
o1=person('rohit',22,17,45)
o1.getBMIresult()

OUTPUT:

You are obse

You might also like