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.

The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static binding - the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program.

 

Pointer to object 

 

Example of pointer to object  

#include<iostream.h>

#include<conio.h>

class A{

public:

            void display(){

                        cout<<"display\n";                                 

            }

};

void main(){

            clrscr();

            A obj,*p;

            p=&obj;

            p->display();

getch();

}

Output

        Display


Types of Polymorphism

     1. Static Binding
         2. Dynamic Binding


Static binding  

When method calling fix at compile-time called static binding, compile-time binding and early binding.

 

Example of static binding  

#include<iostream.h>

#include<conio.h>

class A{

public:

            void display(){

                        cout<<"A \n";

            }

};

class B:public A{

public:

            void display(){

                        cout<<"B \n";

            }

};

void main(){

            A *p;

            A obj;

            B obj1;

            clrscr();

            p=&obj;

            p->display();

            p=&obj1;

            p->display();

getch();

}

Output

        A

        A


Dynamic binding  

When method calling fix at run-time called dynamic binding, run-time binding and late binding.

 

Example of dynamic binding  

#include<iostream.h>

#include<conio.h>

class A{

public:

            virtual void display(){

                        cout<<"A \n";

            }

};

class B:public A{

public:

            void display(){

                        cout<<"B \n";

            }

};

void main(){

            A *p;

            A obj;

            B obj1;

            clrscr();

            p=&obj;

            p->display();

            p=&obj1;

p->display();

getch();

}

Output

        A

        B


Virtual function  

A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.

What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic binding, or late binding.

 

Example of virtual function  

#include<iostream.h>

#include<conio.h>

class A{

public:

            virtual void display(){

                        cout<<"A \n";

            }

};

class B:public A{

public:

            void display(){

                        cout<<"B \n";

            }

};

void main(){

            A *p;

            A obj;

            B obj1;

            clrscr();

            p=&obj;

            p->display();

            p=&obj1;

            p->display();

getch();

}

Output

        A

        B


Pure Virtual Functions 

Is a virtual function which we need not to write any function definition and only we have to declare it. It is declared by assigning ‘0’ in the declaration. It is possible that you want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

Class which have pure virtual function called abstract class we can not create a abstract class. Derived class of abstract class which override pure virtual function called concreate class.

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

 

Example of pure virtual function 

#include<iostream.h>

#include<conio.h>

class A{

public:

            virtual void display()=0;

};

class B:public A{

public:

            void display(){

                        cout<<"B\n";

            }

};

void main(){

            A *p;

            B obj;

            clrscr();

            p=&obj;

            p->display();

getch();

}

Output

        B