C++ :Polymorphism |Run time Polymorphism

Note :
Normally , “A pointer variable is a variable which can store address of another variable of its own type”.

With reference to inheritance this definition is modified a little bit. After modification it says that

“A pointer variable of base class can also store the address of the object of the derived class.”

In static binding, the type of pointer variable is more important than the contents of the pointer variable.

Whereas

In dynamic binding, the contents of the pointer variable is more important than the type of pointer variable.

Early / static binding with inheritance

Example:
There is one base class and two derived classes all the classes have a member function with the name display.

#include<iostream>
#include<conio.h>
using namespace std;
class base
{
	public:
		void display()
		{
			cout<<"Display function of class base"<<endl;
		}
};
class derived1:public base
{
	public:
		void display()
		{
			cout<<"Display function of class derived1 "<<endl;
		}
};
class derived2:public base
{
	public:
		void display()
		{
			cout<<"Display function of class derived2"<<endl;
		}
};
int main()
{
	base b1,*b2;
	derived1 d1;
	derived2 d2;
	b2=&b1;
	b2->display();
	b2=&d1;
	b2->display();
	b2=&d2;
	b2->display();
	getch();
	return(0);
}

Output:

Display function of class base
Display function of class base
Display function of class base

In the above program static binding is performed. So the type of pointer variable is more important than the contents of the pointer variable.
Since the pointer variable is of base class type, so irrespective of the address which is stored in b2 (pointer variable) it always invokes the base class member function display().