Inheritance in Python
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
#Inheritance in Python
#
# class First:
# def add(self,a,b):
# return(a+b)
# class Second(First):
# def sub(self,a,b):
# return(a-b)
# def multi(self,a,b):
# return(a*b)
# # calling
# s=Second()
# print(s.sub(10,5))
# print(s.multi(10,5))
# print(s.add(10,5))
# # Multilevel Inheritance
# class First:
# def add(self,a,b):
# return(a+b)
# class Second(First):
# def sub(self,a,b):
# return(a-b)
# class Third(Second):
# def Multi(self,a,b):
# return(a*b)
# # Calling
# T=Third()
# x=int(input("Enter First No"))
# y=int(input("Enter Second No"))
# print(T.add(x,y))
# print(T.sub(x,y))
# print(T.Multi(x,y))
# Multiple Inheritance
# class Cirlce:
# def areaC(self,r):
# A=3.14*r*r
# return(A)
# def circum(self,r):
# c=2*3.14*r
# return(c)
# class Rect:
# def areaR(self,l,b):
# return(l*b)
# def Paremeter(self,l,b):
# return(2*(l+b))
# class Myclass(Cirlce,Rect):
# None
# #Calling
# M=Myclass()
# print(M.areaC(12.33))
# print(M.areaR(12.33,12.44))
# print(M.circum(12.55))
# print(M.Paremeter(12.33,12.44))
# Hybridge Inheritance
class Student:
def getStudent(self,rno,name,course):
self.rno=rno
self.name=name
self.course=course
class SubMarks(Student):
def getMarks(self,eng,hindi,comp):
self.eng=eng
self.hindi=hindi
self.comp=comp
class Sports:
def getSports(self,sp):
self.sp=sp
class Result(SubMarks,Sports):
def display(self):
print("Rno: ",self.rno)
print("Name: ",self.name)
print("Course: ",self.course)
print("Hindi :",self.hindi)
print("English :",self.eng)
print("Computer :",self.comp)
print("Sports Marks: ",self.sp)
total=self.eng+self.hindi+self.comp+self.sp
print("Total Marks=: ", total)
per=total//4
print("Precentage is =: ", per)
# calling
R=Result()
R.getStudent(101,"Vijay Verma","BTech")
R.getMarks(80,78,88)
R.getSports(80)
R.display()
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

