Polymorphism function overloading and operator overloading
Polymorphism function overloading and operator overloading
2 Polymorphism
Polymorphism means something having multiple or
many forms. In C++ programming language, polymorphism is
also known as overloading. C++ allows to create multiple
functions of the same name and also give new meaning
to already defined operators.
You can create one complete set of functions with the same
name that are capable of performing different operations.
These functions consists of different set of argument lists that
is used to differentiate between them and the kind of
operations they can perform also depends on the argument list
of each function.
Compiler is able to bind the function with appropriate function
call statement at the compile time using the number of
arguments in the argument list and the type of arguments.
Compiler in C++ programming language cannot differentiate
between the two functions with same name on the basic of
their return type.
Program 1
To find the sum of two numbers
// header files
//definition of class
class add
private:
int var1 ;
int var2 ;
public:
add ( )
var1 = 0 ;
var2 = 0 ;
var1 = a ;
var2 = b ;
void display ( )
};
void main( )
add obj1;
float a = 12.5 , b = 13.3 ;
obj1.set ( a , b ) ;
obj1.display ( ) ;
getch () ;
add obj1;
obj1.set ( a , b ) ;
var1 = a ;
var2 = b ;
}
Wait! The formal arguments used in the member
function are of integer data type and actual arguments
passed in the function call statement are of float type.
The answer to the question whether the program will
execute successfully or not is, yes.
Output:
Value of data member var1 :- 12
Program 2
To implement overloading of sum function (keeping type
of arguments different)
// header files
# include < iostream.h >
# include < conio.h >
//definition of class
class add
{
private:
int var1 ;
int var2 ;
float var3 ;
float var4 ;
public:
// default constructor function
add ( )
{
var1 = 0 ;
var2 = 0 ;
}
// overloaded function with integer arguments
int sum ( int a , int b )
{
cout<< " \n Function to add two integer numbers " ;
var1 = a ;
var2 = b ;
return ( var1 + var2 ) ;
}
// overloaded function with float arguments
float sum ( float a , float b )
{
cout<< \n Function to add two float numbers " ;
var3 = a ;
var4 = b ;
return ( var3 + var4 ) ;
}
};
void main ( )
{
// object of class add
add obj1;
float fa = 12.5 , fb = 13.3 , ftotal ;
int ia = 18 , ib = 20 , itotal ;
// call to overloaded member function using float arguments
ftotal = obj1.sum ( fa , fb ) ;
cout<< " \n Sum of " << fa << " and " << fb << " is " <<
ftotal ;
// call to overloaded member function using integer arguments
itotal = obj1.sum ( ia , ib ) ;
cout<< " \n Sum of " << ia << " and " << ib << " is " <<
itotal ;
getch ( ) ;
}
Both the functions share the same name. The first instance
accepts two integer arguments and the second instance
accepts two float arguments. Following function call statement
will result in the execution of member function with formal
arguments of integer type:
itotal = obj1.sum ( ia , ib ) ;
Because the actual arguments are also of the integer type.
Similarly, the following function call statement will result in the
execution of member function with formal arguments of float
type:
ftotal = obj1.sum ( fa , fb ) ;
Because the actual arguments are also of float type.
Output:
Function to add two float numbers
Sum of 12.5 and 13.3 is 25.8
Function to add two integer numbers
Sum of 18 and 20 is 38
Program 3
To implement overloading of average function (keeping
number of arguments different)
// header files
# include < iostream.h >
# include < conio.h >
// definition of class
class average
{
private:
int var1 ;
int var2 ;
int var3 ;
public:
// default constructor function
average ( )
{
var1 = 0 ;
var2 = 0 ;
var3 = 0 ;
}
// overloaded function with 2 integer arguments
int compute ( int a , int b )
{
cout<< " \n Function to find average of two integer
numbers " ;
var1 = a ;
var2 = b ;
return ( ( var1 + var2 ) / 2 ) ;
}
// overloaded function with 3 integer arguments
int compute ( int a , int b , int c )
{
cout<< " \n Function to find average of three integer
numbers " ;
var1 = a ;
var2 = b ;
var3 = c ;
return ( ( var1 + var2 + var3 ) / 3 ) ;
}
};
void main ( )
{
// object of class average
average obj1 ;
int a = 10 , b = 20 , c = 30 , avg ;
// call to overloaded member function using 2 arguments
avg = obj1.compute ( a , b ) ;
cout<< " \n Average of " << a << " and " << b << " is " <<
avg ;
// call to overloaded member function using 3 arguments
avg = obj1.compute ( a , b , c ) ;
cout<< " \n Average of " << a << " , " << b << " and " << c
<< " is " << avg ;
getch ( ) ;
}
Both the functions share the same name. The first definition
accepts two integer arguments and the second instance
accepts three integer arguments. Following function call
statement will result in the execution of member function with
formal arguments of 2 integers:
avg = obj1.compute ( a , b ) ;
and the following function call statement will result in the
execution of member function with formal arguments of 3
integers:
avg = obj1.compute ( a , b , c ) ;
Output:
Function to find average of two integer numbers
Average of 10 and 20 is 15
Function to find average of three integer numbers
Average of 10, 20 and 30 is 20
Program 4
To implement constructor overloading
// header files
# include < iostream.h >
# include < conio.h >
// definition of class
class add
{
private:
int var1 ;
int var2 ;
public:
// default constructor function
add ( )
{
cout<<" \n Default constructor called " ;
var1 = 0 ;
var2 = 0 ;
}
// parameterized constructor function
add ( int a , int b )
{
cout<<" \n Parameterized constructor called " ;
var1 = a ;
var2 = b ;
}
// copy constructor function
add ( add & obj )
{
cout<<" \n Copy constructor called " ;
var1 = obj.var1 ;
var2 = obj.var2 ;
}
void display ( )
{
cout<< " \n Value of var1 = " << var1 << " and var2
= " << var2 ;
}
};
void main ( )
{
// objects of class add
add obj1;
add obj2 ( 10 , 20 ) ;
add obj3 ( obj2 ) ;
cout<< " \n Value of data members with default constructor " ;
obj1.display ( ) ;
cout<< " \n Value of data members with parameterized
constructor " ;
obj2.display ( ) ;
cout<< " \n Value of data members with copy constructor " ;
obj3.display ( ) ;
getch ( ) ;
}
Output:
Now, you are clear with the concept of operator and its
operating rules and regulations. Operator overloading is the
concept of giving new meanings to already available operators
in C++. Overloading an operator does not change the basic
meaning of the operator. The overloaded operator still behaves
the same when it is applied to non class objects or types like
integer, float or char.
Only the operators that are pre-defined by C++ compiler can
be overloaded. Overloading is done to give new meaning to
these operators and operator overloading cannot be used to
create or define new operators. Overloading an operator is
restricted to giving additional meaning to the operator and you
cannot change the behavioral attributes of the operator. By
behavioral attributes we mean the precedence, associativity
and syntax for the operator.
Let us consider that plus '+' operator has been overloaded for
some class and Class_Object1, Class_Object2, and
Class_Object3 are 3 objects of the same class. '+' in the last
statement is treated as function called operator function.
Operator function is discussed in the next section. Interestingly
not all operators available in C++ can be overloaded.
Program 1
To overload the unary minus '-' operator
//header files
#include<iostream.h>
#include<conio.h>
//class definition
class MINUS
{
private:
int var1;
public:
//constructor
MINUS ()
{
var1 =0;
}
void input()
{
cout<<”\nEnter the value of member var1 :- “;
cin>>var1;
}
// definition of operator function for unary '-' operator
MINUS operator - ()
{
MINUS temp;
temp.var1 = -var1;
return temp;
}
void display()
{
cout<<"\nValue of member var1 :- "<<var1;
}
};
void main()
{
//objects of class MINUS
MINUS obj1;
//call to member function of obj1
obj1.input();
//call to operator function for overloading unary minus '-'
operator
obj1 = -obj1;
//call to member function of obj1
obj2.display();
getch();
}
Following is the operator function used in the last program to
overload the unary minus '-' operator:
MINUS operator - ()
{
MINUS temp;
temp.var1 = -var1;
return temp;
}
Return type is the class type itself. The statement used to call
the operator function:
Obj1 = -obj1;
Here the call to the operator function is done for the object obj1
and the temp object returned in the operator function is
assigned to obj1.
Output:
Program 2
To implement increment operator '++' using single
operator function
//header files
#include <iostream.h>
#include<conio.h>
//definition of class
class INC
{
private:
int var1;
public:
//constructor function
INC( )
{
var1= 0;
}
void input ( )
{
cout<<”\nEnter value for data member var1 :- “;
cin>>var1;
}
// definition of operator function to overload increment
operator '++'
void operator ++ ( )
{
var1++;
}
void display( )
{
cout<<”\nValue of data member var1 :- “<<var1;
}
};
void main( )
{
// object of class INC
INC obj1;
//call to member function of obj1
obj1.input( );
// call to operator function '++'
obj1++;
// call to operator function '++'
++obj1;
The first statement uses the post increment scheme and the
second statement uses the pre increment scheme. Because the
program is written using a single operator function and the
program also does not explicitly differentiate between pre and
post increment, same operator function is executed for both
the operator function call statements.
Output:
Enter value for data member var1 :- 47
Value of data member var1 :- 49
//header files
#include <iostream.h>
#include<conio.h>
//class defintion
class INC
{
private:
int var1;
public:
//constructor function
unaryinc ( )
{
var1 = 0;
}
void input ( )
{
cout<<”\nEnter value of data member var1 :- “;
cin>>var1;
}
// definition of operator function for pre increment
scheme
INC operator ++ ( )
{
INC temp;
var1++;
temp.var1 = var1;
return temp;
}
// definition of operator function for post increment
scheme
INC operator ++ ( int )
{
INC temp;
temp.var1 = var1;
var1++;
return temp;
}
void display( )
{
cout<<”\nValue of member function var1 :- “<<var1;
}
};
void main( )
{
// objects of class INC
INC obj1, obj2;
cout<<”\nObject 1”;
//call to memebr function of obj1
obj1.input( );
// call to pre increment operator function
obj2 = ++obj1;
cout<<”\nValue of data member var1 of object1 after pre
increment”;
obj1.display();
cout<<”\nValue of data member var1 of object2 after pre
increment”;
obj2.display();
// call to post increment operator function
obj2 = obj1++;
cout<<”\nValue of data member var1 of object1 after post
increment”;
obj1.display();
cout<<”\nValue of data member var1 of object2 after post
increment”;
obj2.display();
getch();
}
Output:
Object 1
Enter value of data member var1 :- 10
Program 4
To overload binary operator plus '+'
//header files
#include <iostream.h>
#include<conio.h>
//class definition
class ADD
{
private:
int var1;
int var2;
public:
//constructor function
ADD( )
{
var1 = 0;
var2 = 0;
}
void input ( )
{
cout<<”\nEnter value of data member var1 :- “;
cin>>var1;
void main( )
{
ADD obj1, obj2, obj3;
cout<<”\nObject 2”;
//call to member function of object 2
obj2.input( );
cout<<”\nObject 3”;
//call to member function of object 3
obj3.input( );
// call to the operator function '+'
obj1 = obj2 + obj3;
cout<<”\nObject 1 after addition”;
obj1.display();
getch();
}
Output:
Object 2
Enter value of data member var1 :- 10
Enter value of data member var2 :- 12
Object 3
Enter value of data member var1 :- 11
Enter value of data member var2 :- 14