C++ :Polymorphism |Run time Polymorphism

Dynamic/Late binding with inheritance (virtual function)


* A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.
* It is used to tell the compiler to perform dynamic linkage or late binding on the function.
* There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the ‘virtual’ function.
* A ‘virtual’ is a keyword preceding the normal declaration of a function.
* When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

Note:

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

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

// dynamic binding with inheritance
// using virtual function
#include<iostream>
#include<conio.h>
using namespace std;
class base
{
	public:
		virtual 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 derived1
Display function of class derived2

In the above program, dynamic binding is performed. So the type of pointer variable is not important but the contents of the pointer variable are important.

In the first case since the pointer variable contains the address of the base class object, it invokes the display function of base class.

In the second case the pointer variable contains the address of the object of the class derived1, so the display function of the deriverd1 class is invoked.

In the third case, the pointer variable contains the address of the object of the class derived2, so the display function of the deriverd2 class is invoked.

Rules of Virtual Function

* Virtual functions must be members of some class.
* Virtual functions cannot be static members.
* They are accessed through object pointers.
* They can be a friend of another class.
* A virtual function must be defined in the base class, even though it is not used.
* The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
* We cannot have a virtual constructor, but we can have a virtual destructor

Differences between compile time and run time polymorphism.

Compile-time polymorphism Run time polymorphism
The function to be invoked is known at the compile time. The function to be invoked is known at the run time. It is also known as overloading, early binding, and static binding. It is also known as overriding, Dynamic binding, and late binding.
Overloading is a compile-time polymorphism where more than one method is having the same name but with a different number of parameters or the type of parameters. Overriding is a run time polymorphism where more than one method is having the same name, number of parameters, and type of parameters.
It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers.
It provides fast execution as it is known at the compile time. It provides slow execution as it is known at the run time.
It is less flexible as mainly all the things execute at the compile time. It is more flexible as all the things execute at the run time.