122122, 9:12 Inheritance neha - Jupyler Notebook
In [1]:
1 #Single Inheritance
2 class Student(): #base class
3 def _init_(self): #constructor function
4 self.__rno=int(input ("Enter Rollno."))
5 self._name=input ("Enter Name : ")
6 self.__age=int(input("Enter Age : "))
7 self.__city=input("Enter City : ")
8 def _del_(self): #destructor function
9 pass
10 def display(self
1 print("Rollno. : “,self._rno)
2 print("Nane : ",self._name)
B print("Age : ",self. age)
14 print("City : ",self._city)
In [2]:
1 class Marks(Student): #here Marks is derived means child class which inherits
2 #the features of parent means base class
3 def _init_(self): #constructor function
4 Super()-__init__() #caLl member constructor function of parent
5 #super() inbuilt function which is used for parent class
6 self.__phy=float(input ("Enter marks of Physics "))
7 self.__chem=float(input("Enter marks of Chemistry "))
8 self._maths=float(input("Enter marks of Maths "))
9
10 def _del_(self): #destructor function
1 pass
2 def display(self):
B super().display() #call member of parent
14 print("Marks in Physics : ",self._phy)
15 print("Marks in chemistry : ",self._ chem)
16 print("Marks in Maths :",self. maths)
In [3]:
1 ¢main program
2 #create object of derived class Marks
3. mi-Marks()
4 mi.display()
5 del mi
Enter Rollno.45
Enter Name : gj
Enter Age : 45
Enter City : yi
Enter marks of Physics 45
Enter marks of Chemistry 454
Enter marks of Maths 45
Rolino. : 45
Name: gj
Age: 45
city: yi
Marks in Physics : 45.0
Marks in chemistry
454.0
Marks in Maths : 45.0
localhost 8888 /notebooksiinhetance neha ipynb
ano1122/22, 9:12 PM
In [1]:
1 #multi Level inheritance
2 class Student(): #base class
Inheritance neha - Jupyler Notebook
3 def _init_(self): #constructor function
4 self._rno=int(input("Enter Rollno."))
5 self.__name=input("Enter Nane
6 self. __age=int(input ("Enter Age
7 self. _city=input("Enter City :
8 def _del_(self): #destructor function
9 pass
10 def display(self):
uu print("Rollno. : ",self._rno)
2 print("Nane : ",self._name)
B print("Age : ",self.__age)
14 print(“City : ",self._city)
In]:
1
In [2]:
1
In [5]:
1 class Result(Marks): #here redsult class which inherits the feature of the base class f
2 def compute(self):
3 self._total=self._phy+self._maths
4 self._percent=self.__ total*100/300
5 def display(self):
6 super().display() call display() of marks base class
7 print(“Total Marks : “,self._ total)
8 print("percent : “,self._percent)
localhost 8888 Inotebooksiinhentance neha ipynb
ano122122, 9:12 Inheritance neha - Jupyler Notebook
In [6]:
4main program
fcreate object of result class
res=Result()
res.compute()
res.display()
when
Enter Rollno.45
Enter Name : FG
Enter Age : 45
Enter City : GF
Enter marks of Physics 45
Enter marks of Chemistry 54
Enter marks of Maths 54
Rollno. : 45
Name : FG
Age: 45
City: GF
Marks in Physics : 45.0
Marks in chemistry : 54.0
Marks in Maths : 54.0
Total Marks : 99.0
percent : 33.0
In [1]:
1 fmultiple inheritance
2 class Student(): #base class
3 def _init_(self): #constructor function
4 self. _rno=int (input (Enter Rollno."))
5 self.__name=input(“Enter Name :
6 self.__age=int(input("Enter Age : *))
7 self._city=input("Enter City : ")
8 def _del_(self): #destructor function
9 pass
10 def display(self):
11 print("Rollno. : “,self.__rno)
2 print("Nane : ",self._name)
B print("Age : ",self.__age)
14 print("City : ",self._city)
In [6]:
1 class Marks():#base class2
2 def _ init__(self): #constructor function
3 self._phy=float(input("Enter marks of Physics "))
4 self._chem=float(input ("Enter marks of Chemistry “
5 self._maths=float(input("Enter marks of Maths "))
6
7 def _del_(self): #destructor function
8 pass
9 def display(self):
10 print("Marks in Physics : ",self._phy)
11 print("Marks in chemistry : ",self. chem)
2 print("Marks in Maths :",self._maths)
localhost 3888 /notebooksiinhetance neha ipynb
30122122, 9:12 Inheritance neha - Jupyler Notebook
In [7]:
1
2
3
4
5
6
7
8
9
10
1
12
13
14
15
class Result(Student,Marks): #here Result derived class which inherits
atthe features of base class Student and Marks
def _init_(self): #constructor
super()._init_() #caLl constructor function of first base class Student
Marks. init__(self) #call constructor function of Marks base class
def compute(self):
tprint(self.__rno) #error show
#find the total marks
self,__total=self._phy+self._chem+self._maths
self._percent=self.__total*100/300
def display(self):
super().display()#call
Marks. display(self)#call
print(“Total Marks : “,self._ total)
print("PErcent : “,self._percent)
In [8]:
1 res=Result()
2 res.compute()
3. res.display()
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Rolino.45
Name : gh
Age : 56
City : ghhg
marks of Physics 56
marks of Chemistry 65
marks of Maths 56
Rollno. : 45
Name :
Age :
city :
Marks
Marks
Marks
Total
gh
56
ghhg
in Physics : 56.0
in chemistry : 65.0
in Maths : 56.0
Marks : 177.0
Percent : 59.0
In [9]:
1 #Hierarchical Inheritance
2 class Shape: #Shape base class
3
Beavaus
def Vol_cube(self):
azint(input("Enter Side : "))
return atata
def Vol_cuboid(self):
azint(input("Enter Side a :
beint(input("Enter Side b
caint(input("Enter Side c
return atb*c
localhost 3888 !notebooksiinhetance neha ipynb
ano122122, 9:12 Inheritance neha - Jupyler Notebook
In [10]:
1 #Find volume of cube
2 class Derivedi(Shape): #0erivedi is a child class which inherits Base class
3 ‘#Shape
4 def volune(self):
5 vesuper().Vol_cube() #call function of base class
6 print("Volume of cube : ",v)
In [11]:
1 #Find volume of cuboid
2. class Derived2(Shape): #Derived2 is a child class which inherits Base class
3 ‘#Shape
4 def volune(self):
5 vesuper().Vol_cuboid() #call function of base class
6 print("Volume of cuboid : ",v)
In [14]:
#Main program
print("1. Volume of cube")
print("2. Valume of cuboid")
nt(input("Enter Your choice : *))
Hereate object of Derived classi
D1=Derivedi()
Di.volume() #call
elif ch==2:
18 tcreate object of Derived classi
1 D2=Derived2()
2 D2.volume() #call
13 else:
14 print("Invalid choice")
1. Volume of cube
2. Valume of cuboid
Enter Your choice : 2
Enter Side a : 12
Enter Side b : 12
Enter Side c : 12
Volume of cuboid : 1728
In [16]:
1 #Method Overloading
2 class Shape:
3 def area(self,a=None,b-None) :
if alzNone and b=sNone :
return 3.14*a*a #area of circle
elif a!=None and b!=None :
return a%b #area of rectangle
else:
return “invalid”
localhost 3888 /notebooksiinhetance neha ipynb 50122122, 9:12 Inheritance neha - Jupyler Notebook
In [18]:
4main program
#create object of shape class
‘S4=Shape()
ar=S1.area(3)
print("Area of circle :",ar)
print("Area of rectangle :”
print(S1.area())
» St.area(4.5))
Nawnune
Area of circle : 28.259999999999998
Area of rectangle : 63.585
invalid
In [1]:
#operator overloading
ass
BRUNE
arb
out[1]:
In [3]:
1 int._add_(2,5)
out [3]:
7
In [4]:
2 be"6"
3 arb
4
out [4]:
*56°
In [5]:
1 str._add_(‘5','4")#operator Loading magic method
out[S]:
‘sae
In [6]:
2g%
out[6]:
localhost 3888 /notebooksiinhetance neha ipynb
ano1122/22, 9:12 PM
In [10]:
1 bool.__add_(2,2)
out [10]:
4
In [12]:
1 float._add_(2.0,2.0)
out [12]:
4.0
In [13]:
as!
bs
at
out[13]:
1
In [19]:
1 int. _mod_(10,5)
out [19]:
e
In [20]:
1 class a:
def _ init,
self.x=x
def show(self):
print ("x=",self.x)
(self, x):
wawN
In [25]:
fimain program
At=a(10)
A2=a(20)
a3-a(30)
AL. show()
A2.show()
A3.show()
Nouwnune
10
20
30
localhost 3888 !notebooksiinhetance neha ipynb
Inheritance neha - Jupyler Notebook
m072212, 9:12 PM inheritance neha -Jupyler Notebook
In [28]:
sum of 2 nos
4miltiplication of 2 nos.
class A\
def _ init,
Self .x=x
def show(sel#)
print("x=", self.)
def _add_(self,other):
return self.x*other.x
(self,x):
wevanaunn
In [29]:
A1=A(10)
A2=A(20)
AL. show()
2. show)
AL+A2
waune
x= 10
20
out[29]:
200
In [2]:
1 #_str_() #magic method
2 class Employee:
3 “WE ARE CREATING A CLASS EMPLOYEE"
4 def _init_(self):
5 self.name=input("enter name of employee : ")
6 self.salary=int(input("Enter salary of employee : *))
7 def _str_(self):
8 return "Employee name : “sself.name+"\n salary:"+str(self.salary)
9
In [3]:
1 main program
2 d#create object of class Emplyee
3. print(Employee._doc_)
4 E1=Employee()
5 print(E1) #automatic call str magic method
6 print("Dictionary of class: ",£mployee._dict_)
7 #inbuilt attribute of class
8
WE ARE CREATING A CLASS EMPLOYEE
enter name of employee : neha
Enter salary of employee : 100000
Employee name : neha
salary:100000
Dictionary of class: {"_module_': '_main_', '_doc_': 'WE ARE CREATING
A CLASS EMPLOYEE, '_init_': , '_str_': , '__dict
": , '_weakref_': )
localhost 3888 !notebooksiinhetance neha ipynb
ano122122, 9:12 Inheritance neha - Jupyler Notebook
In [12]:
1 #method overloading: always used in inheritance
2 class addition: #base class
3 def show(self):
4 a6
5 b=2
6 caatb
7 print("sum : ",c)
8 class Subtraction(addition): #derived class subtraction which inherits the features of
9 def show(self):
18 super().show()
un
2
B
14 print("subtract: ",e)
In [13]:
1 si=Subtraction()
2 si.show()
sum: 8
subtract: 4
In [14]:
1 #access idendifier of class
2 #1.private:
3 #2. public: by defualut public
4 #protected : it is used only for inheritance
In [15]:
1 #recursion: always used in ser defiend function
2 #recursion means function call itself repeatedly
3 #iteration means Loop
4
In [16]:
1 #wap to find the factorial of given no
2 def fact(n):
3 fal
4 while n>@ : #iteration
5 fof
6 nen-1 #n=3 n=2 n=1 n=O
7 return f
In [17]:
1 neint(input("enter number : "))
2 fefact(n)
3 print("factioal : ",f)
enter number : 45
factioal : 11962222086548019456196316149565771564383733760000000000
localhost 3888 /notebooksiinhetance neha ipynb
ono6122/22, 912m inheritance neha - Jupyler Notebook
In[ J:
localhost 3888 !notebooksiinhetance neha ipynb 10110