Multiple inheritances:
- Multiple inheritances have a number of base classes and one derived class.
- It means the numbers of base classes are derived from one derived class.
Syntax:
Example:
Output:
If condition:
Else condition:
Multiple inheritances:
Syntax:
class a
//Base class1
class b
//Base class2
class c:public a,public b
//Derived class
//Object creation
Object_name=Derived_class_name();
object_name.function_name(parameter_list);
Example:
#Multiple inheritance
class bank:
def pin_set(self):
lst=[1111,1234,2222]
print("\tWelcome to XXX bank")
return lst
class getvalue:
def get_data(self):
pin=int(input("Enter pin number:"))
return pin
class pin_validation(bank,getvalue):
def verification(self,pin,lst):
# for i in lst:
if pin in lst:
print("Enter your
choice:\n1.Cash widrawel\n2.Cash deposite\n3.Mini statement")
else:
print("Invalid pin")
b=pin_validation()
l=[]
l=b.pin_set()
p=b.get_data()
b.verification(p,l)
Output:
If condition:
Else condition:
Multilevel inheritance:
#Multi level inheritance
class bank:
def pin_set(self):
lst=[1111,1234,2222]
print("\tWelcome to XXX bank")
return lst
class getvalue(bank):
def get_data(self):
pin=int(input("Enter pin number:"))
return pin
class pin_validation(getvalue):
def verification(self,pin,lst):
if pin in lst:
print("Enter your
choice:\n1.Cash widrawel\n2.Cash deposite\n3.Mini statement")
else:
print("Invalid pin")
b=pin_validation()
l=[]
l=b.pin_set()
p=b.get_data()
b.verification(p,l)
Inheritance:
Types of Inheritance:
Benefits of inheritance:
Syntax:
class class_name1:
#function definition
class class_name2(class_name1):
#function definition
object_name=class_name(parameter_list)#Object
creation
object_name.function_name(parameter_list)#function
calling
Example:
#inheritance with constructor
class employee:
def __init__(self,id,name,depart,sal):
self.id=id
self.name=name
self.depart=depart
self.sal=sal
def display(self):
print("\tEmployee details")
print("ID :",self.id)
print("Name:",self.name)
print("Department :",self.depart)
print("Basic Salary : ",self.sal)
class salary_cal(employee):
def sal_cal(self)://access sal from base class
if self.sal>=15000:
self.sal+=5000
print("Increased
salary:5000")
print("Net
salary:",self.sal)
elif self.sal>=1000 and self.sal<15000:
self.sal+=3000
print("Increased
salary:3000")
print("Net
salary:",self.sal)
else:
self.sal+=1000
print("Increased
salary:1000")
print("Net
salary:",self.sal)
id=input("Enter employee id:")
name=input("Enter employee
name:")
depart=input("Enter employee
department:")
sal=int(input("Enter basic
salary:"))
#object creation
emp=salary_cal(id,name,depart,sal)
#emp=employee(id,name,depart,sal)
emp.display()
emp.sal_cal()
Output:
1) Single inheritance:
class employee:#base class
def get_data(self):#All function should have the self keyword for joining
other fun
id=input("Enter employee id:")
name=input("Enter employee name:")
depart=input("Enter employee department:")
sal=int(input("Enter basic salary:"))
return id,name,depart,sal
def display(self,id,name,depart,sal):
print("\tEmployee details")
print("ID :",id)
print("Name:",name)
print("Department :",depart)
print("Basic Salary : ",sal)
class salary_cal(employee):#derived
class
def sal_cal(self,sal):
if sal>=15000:
sal+=5000
print("Increased
salary:5000")
print("Net salary:",sal)
elif sal>=1000 and sal<15000:
sal+=3000
print("Increased
salary:3000")
print("Net salary:",sal)
else:
sal+=1000
print("Increased
salary:1000")
print("Net salary:",sal)
#Object creation
emp=salary_cal()
empno,ename,deptno,sal=emp.get_data()
emp.display(empno,ename,deptno,sal)
emp.sal_cal(sal)
Object:
Constructor:
import module_name#optional
class class_name:
def __init__(self,parameter_list):
self.parameter_list=variable
def function_name(self,list_of_patameters):
#Statements
object_name=class_name(parameter_list)#Parameter
list based on constructor
object_name.function_name(list_of_patameters)#Function
calling through object
Example program:
class calculator:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x+self.y
def sub(self):
return self.x-self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
x,y=input("Enter two values:
").split()
c=calculator(int(x),int(y))
print("Add value:",c.add())
print("Sub value:",c.sub())
print("Mul value:",c.mul())
print("Div value:",c.div())
Output:
Introductions of Class:
import module_name#optional
class class_name:
def function_name(list_of_patameters):
#Statements
function_name(list_of_patameters)#Function calling
Example program:
class calculator:
def add(x,y):
return x+y
def sub(x,y):
return x-y
def mul(x,y):
return x*y
def div(x,y):
return x/y
a,b=input("Enter two values: ").split()
print("Add value:",add(int(a),int(b)))
print("Sub value:",sub(int(a),int(b)))
print("Mul value:",mul(int(a),int(b)))
print("Div value:",div(int(a),int(b)))
Output:
What is OOPs?
Problems in C and overcome by OOPS:
Uses of oops or why it used?
Create and import custom module in python:
Save the program as a calculation.c
def sqr(n):
return n*n
def cube(n):
return n*n*n
Created another program Example.py for file inclusion:
import calculation
x=int(input("Enter a value to find
square value:"))
print("%d square value
is:%d"%(x,calculation.sqr(x)))
y=int(input("Enter a value to find
cube value:"))
print("%d cube value
is:%d"%(y,calculation.cube(y)))
Output:
Example program for reading, write and
append:
#Write functions to create a text file
called student.txt to store few student details in a list of values
#like enrollment number, name, gender,
standard, and section. Also, write functions for the following
#A. Append
#S. Search
#D.Display
#E.Exit
Program:
def CREATE():
f=open("D:\\KURSHITHA\\Notepad\\student.det","w")
ch=1
while ch:
roll=int(input("Enter Roll Number:"))
name=input("Enter name:")
gen=input("Enter gender:")
std=int(input("Enter std:"))
sec=input("Enter Section:")
rec=str(roll)+" "+name+" "+gen+"
"+str(std)+" "+sec
f.write(rec)
f.write('\n')
ch=int(input("Any more records (1 or 0 ):"))
f.close()
def APPEND():
f=open("D:\\KURSHITHA\\Notepad\\student.det","a")
ch=1
while ch:
roll=int(input("Enter Roll
Number:"))
name=input("Enter name:")
gen=input("Enter gender:")
std=int(input("Enter std:"))
sec=input("Enter Section:")
rec=str(roll)+" "+name+" "+gen+"
"+str(std)+" "+sec
f.write(rec)
f.write('\n')
ch=int(input("Any more records (1 or 0 ):"))
f.close()
def PRINT():
f=open("D:\\KURSHITHA\\Notepad\\student.det","r")
s=" "
print("Roll Name Gender STD Section:")
while s:
s=f.readline()
s=s.rstrip("\n")
a=s.split(" ")
for i in a:
print(i,end='\t')
print()
f.close()
def SEARCH():
f=open("D:\\KURSHITHA\\Notepad\\student.det","r")
s=" "
flag=0
sroll=input("Enter the searching roll number:")
while s:
s=f.readline()
if sroll in s:
print("Searching record
details:",s)
flag+=1
if flag==0:
print("searching record not found")
f.close()
CREATE()#invoke create function
while True:
print("A. Append\nS. Search\nD. Display\nE. Exit")
ch=input("Enter Your choice:")
if ch in 'Aa':
APPEND()
elif ch in 'sS':
SEARCH()
elif ch in 'dD':
PRINT()
elif ch in 'eE':
break
else:
print("Invalid choice")
Output:
Before running the program:
After running the program: