C++ – Dynamic Polymorphism

Hello Friends,

Today, in this article we will mostly discuss about Dynamic Polymorphism and the difference between Static Polymorphism and Dynamic Polymorphism .

Static Polymorphism : If function call binding is performed at compile time. This is also known as Compile time binding / Early binding. (Please refer to previous blog for details of Static Polymorphism :  C++ – Static Polymorphism  )

Dynamic Polymorphism : If function call binding is performed at run time. This is also known as Run-time binding / Late binding.

Purpose of Dynamic Polymorphism :

  • We can store derived class object in a base class pointer .
  • Assume that : same named function is available in base class as well as derived classes (which is called function overriding , achieved by declaring method as virtual ).
  • If we store a derived class object in a base class pointer and call a particular function through the base class pointer , the expectation is : it should call the function which is  available in derived class (whose address is stored in base class pointer ) instead of calling the function which is available in base class . This is the goal of Dynamic polymorphism .

Let’s analyse the below program.

Sample code 1

class base
{
	public:
	void show()
	{
		cout<<"user in base class"<<endl;
	}
};

class derived:public base
{
	public:
	void show()
	{
		cout<<"user in derived class"<<endl; 
	} 
}; 

int main() 
{ 
    derived child; 
	base *p = new base;  /*Base class pointer which points to base class object*/ 
	p->show();        //Call base class show() method
	p = &child;     /*base class pointer stores the address of derived class object child*/
	p->show();       //Call base class show() method
	getchar();
	return 0;
}

O/P -> user in base class                                                                                                                    user in base class

As the ideal case , we will expect that after storing the derived class object in base class pointer , the show method call should call the derived class show method . But it didn’t happen .

Here the function binding happens at compile time . As the pointer is base type , compiler will bind the function call to base class “show()” method .

In this case to call the method of the actual stored object,  we have to declare the method as virtual which lead to late binding instead of compile time binding .

Note * -> We will discuss details of virtual keyword and how compiler internally handles this in next blog .

Program using virtual method:

class base
{
	public:
	virtual void show()
	{
		cout<<"user in base class"<<endl;
	}
};

class derived:public base
{
	public:
	void show() override
	{
		cout<<"user in derived class"<<endl; 
	} 
}; 

int main() 
{ 
	derived child; 
	base *p = new base; 
	p->show();
	p = &child;
	p->show();     //This will call derived class show() method
	getchar();

	return 0;
}

O/P -> user in base class                                                                                                                      user in derived class

Difference between Static Polymorphism & Dynamic Polymorphism

Static Polymorphism Dynamic Polymorphism
In Compile time Polymorphism, call is resolved by the compiler. In Run time Polymorphism, call is not resolved by the compiler.
It is also known as Static binding, Early binding and overloading as well. It is also known as Dynamic binding, Late binding and overriding as well.
Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature . Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class with virtual method & its subclass.
It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers.
It provides fast execution because known early at compile time. It provides slow execution as compare to early binding because it is known at runtime.

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

 

You may also like...

1 Response

  1. October 14, 2018

    […] The virtual mechanism mostly used to accomplish the dynamic polymorphism, which we discusses in one of the previous blog C++ – Dynamic Polymorphism. […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: