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

Function overloading or Polymorphism.pdf

  • 1.
    DAP TOPICS UNIT 3 Functionoverloading , Operator Overloading
  • 2.
    FUNCTION OVERLOADING Function overloadingis 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.
  • 3.
    EXAMPLE (INCORRECT) Code: Output classFunoverload: 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
  • 4.
    METHOD OVERLOADING IN PYTHONUSING 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
  • 5.
    OPERATOR OVERLOADING It isbasic 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
  • 6.
    SPECIAL METHODS FOR OPERATOROVERLOADING 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.
  • 7.
  • 8.
    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
  • 9.
  • 10.
    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
  • 11.
    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