Function overloading or Polymorphism.pdf

swati463221 9 views 11 slides Nov 15, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

ok


Slide Content

DAP TOPICS
UNIT 3
Function overloading ,
Operator Overloading

FUNCTION OVERLOADING
Function overloading is functions with same name but different number of
arguments and their type
E.g
add(self,a,b)
add(self,a,b,c)
First add() taking 3arguments and other add() taking 4 arguments
Python doesn’t allow method overloading unlike C++,Java.
It doesn’t support overloading based on type as it is not strongly typed language.

EXAMPLE (INCORRECT)
Code:Output
class Funoverload:
def add(self,a,b)
print a+b
def add(self,a,b,c)
print a+b+c
a=Funoverload()
a.add(10,20,30)

Note : As python does not support method overloading by type. So above code generate
error

METHOD OVERLOADING IN
PYTHON USING DEFAULT
ARGUMENTS
class Shape:
def area(self,length=0,breadth=0):
if breadth==0:
ar=length*length #area of square
else:
ar=length*breadth # area of rectangle
print("area is",ar)
obj=Shape()
obj.area(3) # calling method with 1 parameter
obj.area(3,4) # calling method with 2 parameter
obj.area() # calling method with 0 parameter

Method overloading is that in which there are more than
One ways of calling same function

OPERATOR OVERLOADING
It is basic defining data type with its own definition of operators
Best features of programming languages
Makes programmer to interact with objects in natural way.
Operators that can be overloaded:
Arithmetic Comparison
Indexing Slicing
Overload inbuilt functions:
length type conversion

SPECIAL METHODS FOR
OPERATOR OVERLOADING
For operator overloading ,python provides special methods with each
inbuilt functions and operators.
These methods are called while evaluating those operators
Example
Special method for + operator is __ add__()
So if you write, x+y ,it will call __add__() while evaluating __add__().
Similarly to override other operators like -,>,< you have to override corresponding
methods.

SPECIAL METHODS FOR
ARITHMETIC OPERATORS

EXAMPLE OF OVERLOADING +
OPERATOR
class Complex:
def __init__(self,a=0,b=0):
self.r=a
self.i=b
def __add__(self,c1): #override this method to
self.r=self.r+c1.r #overload + operator
self.i=self.i+c1.i
return self
def display(self):
print(self.r," ",self.i)
Program to add two complex numbers by overloading + operator
d1=Complex(10,4) #create complex objects
d2=Complex(1,3)
d3=Complex()
d1.display()
d3=d1+d2 # adding two complex objects
d3.display()

Output

SPECIAL METHODS FOR
COMPARISON OPERATORS

OVERLOADING COMPARISON
OPERATORS
class distance:
def __init__(self, x,y):
self.ft=x
self.inch=y

def __eq__(self, other): # override this method to overload == operator
if self.ft==other.ft and self.inch==other.inch:
return "both objects are equal"
else:
return "both objects are not equal“

def __ge__(self, other): # override this method to overload >g= operator
in1=self.ft*12+self.inch
in2=other.ft*12+other.inch
if in1>=in2:
return "first object greater than or equal to other"
else:
return "first object smaller than other"
Program to overload == to check distance objects are equal or not and >= to find greater object

OVERLOADING COMPARISON
OPERATORS
d1=distance(5,5) # create distance object with 5ft and 5inches
d2=distance(5,5) # create distance object with 5ft and 5inches
print (d1==d2) # check d1 is equal to d2
d3=distance(2,3) # create distance object with 2ft and 3inches
print(d3>=d1) # check d3 is greater than d1
Create objects of distance class and use operators on them
Output
Tags