The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Consider the following example where a base class has been derived by other two classes:

polymorphism is divided into two parts.

(1)Static Binding  (Early Binding)

  • Constructor Overloading
  • Function Overloading
  • Operator Overloading

(2)Dynamic Binding  (late Binding)

Example of Polymorphism:  (operator Overloading)

#include<iostream.h>

#include<conio.h>

class A

{

    int a;

    public:

    A()

    {

        a=x;
        cout<<a;
    }
    void operator +(A a2)
    {
        cout<<" value ="<<a+a2.a;
    }
    void operator *(A a1)
    {
        cout<<" New value="<<a*a2.a;
    }
    int operator -(A a1)
    {
        cout<<" New value="<<a-a2.a;
    }
    void operator /(A a1)
    {
        cout<<" New value="<<a/a2.a;
    }
}a1,a2(5);
void main()
{
    clrscr();
    a1+a2;
    a1*a2;
    int ans=a1-a2;
    cout<<"answer="<<ans;
    a2/5;
    getch();
}