Polymorphism Means More Than One Function With Same Name
Polymorphism Means More Than One Function With Same Name
More than one function with same name, with different signature in a class or in a same scope is
called function overloading. Signature of function includes :
Number of arguments
Type of arguments
Sequence of arguments
When you call an overloaded function, the compiler determines the most appropriate definition
to use by comparing the signature of calling statement with the signature specified in the
definitions.
#include<iostream.h>
#include<conio.h>
class CalculateArea
{
public:
void Area(int r) //Overloaded Function 1
{
cout<<"\n\tArea of Circle is : "<<3.14*r*r;
}
};
void main()
{
CalculateArea C;
C.Area(5); //Statement 1
C.Area(5,3); //Statement 2
C.Area(7,2.1f); //Statement 3
C.Area(4.7f,2); //Statement 4
Output :
In the above example, we have four member functions named Area. Statement 1 will invoke the
function 1 b'coz the signature of function 1 is similar to the statement 1. Similarly Statement 3
will invoke function 4 b'coz statement 3 is passing two arguments, 1st is of integer type and 2nd
is of float type. Function 4 is the only function who is receiving integer and float respectively.
Operator Overloading : Another example of static polymorphism is Operator
overloading. Operator overloading is a way of providing new implementation of existing
operators to work with user-defined data types.
An operator is a symbol that tells the compiler to perform specific task. Every operator have their
own functionality to work with built-in data types. Class is user-defined data type and compiler
doesn't understand, how to use operators with user-defined data types. To use operators with
user-defined data types, they need to be overload according to a programmer's requirement.
An operator can be overloaded by defining a function to it. The function for operator is declared
by using the operator keyword followed by the operator.
Binary operator is an operator that takes two operand(variable). Binary operator overloading is
similar to unary operator overloading except that a binary operator overloading requires an
additional parameter.
Binary Operators
Example
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}
R.L = L + Rec.L;
R.B = B + Rec.B;
return R;
}
void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects
cout<<"\n\tRectangle 1 : ";
R1.Display();
cout<<"\n\n\tRectangle 2 : ";
R2.Display();
R3 = R1 + R2; Statement 1
cout<<"\n\n\tRectangle 3 : ";
R3.Display();
}
Output :
Rectangle 1 :
L : 2
B : 5
Rectangle 2 :
L : 3
B : 4
Rectangle 3 :
L : 5
B : 9
In statement 1, Left object R1 will invoke operator+() function and right object R2 is passing as
argument.
Another way of calling binary operator overloading function is to call like a normal member
function as follows
R3 = R1.operator+ ( R2 );
Unary operator is an operator that takes single operand(variable). Both increment(++) and
decrement(--) operators are unary operators.
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R;
//Creating Object
R++;
Output :
Giving new implementation of base class method into derived class and the calling of this new
implemented function with derived class's object is called function overriding.
Giving new implementation of derived class method into base class and the calling of this new
implemented function with base class's object is done by making base class function as virtual
function.
Virtual function is used in situation, when we need to invoke derived class function using base
class pointer. We must declare base class function as virtual using virtual keyword preceding its
normal declaration. The base class object must be of pointer type so that we can dynamically
replace the address of base class function with derived class function. This is how we can
achieve "Runtime Polymorphism".
If we doesn't use virtual keyword in base class, base class pointer will always execute function
defined in base class.
class BaseClass
{
public:
virtual void Display()
{
cout<<"\n\tThis is Display() method of Base Class";
}
void Show()
{
cout<<"\n\tThis is Show() method of Base Class";
}
};
public:
void Display()
{
cout<<"\n\tThis is Display() method of Derived Class";
}
void Show()
{
cout<<"\n\tThis is Show() method of Derived Class";
}
};
void main()
{
DerivedClass D;
B=&D;
Output :