POLYMORPHISM

From WikiEducator
Jump to: navigation, search

-> It is the ability for a message or data to be processed in more than one form.

-> It is the property by which same message can be sent to objects of several different classes.

Polymorphism is illustrated through the concept of function overloading.

Function Overloading : It is the process of using same name for different function definitions that are differentiable by the number and types of argument.

A function having several definitions that are differentiable by the number or types of their arguments is known as an overloaded function .

Example:

A same function AREA ( ) is being used to calculate the area of different shapes :


#include <iostream.h>

class SHAPE {

   public:
               
void AREA(int l) // Function Definition to calculate area of Square
{
cout << "Area of Square is : " << l*l << endl;
}
void AREA (int l,int b) //Function Definition to calculate area of rectangle
{
cout << "Area of Rectangle is : " << l*b << endl;
}


};


void main( )
{

      
SHAPE s;
s.AREA (10); // Function call to calculate area of Square
s.AREA(5,10); //Function call to calculate area of Rectangle


}