As we Know that the Polymorphism is the feature of OOP,which means we use the same name but multiple forms.
So in case of Run time Polymorphism the Virtual Functions Are used which tell about the binding of function procedure with its call at run time.
Virtual Functions done this by checking the object to which the pointer is pointing rather than the type of pointer.
Following is a Simple program to illustrate the above concept
#include<iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"Display base\n";
}
virtual void show()
{
cout<<"Base Virtual\n";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"Display Derived\n"; }
void show()
{
cout<<"Derived Virtual\n";
}
};
main()
{
Base b;
Derived d;
Base *p=&b;
p->display();
p->show();
p=&d;
p->display();
p->show();
}
Output:
So as we can see in the output the Display function of base class is called two times because it is not virtual
On the other hand the show function is called for both derived and base class because it is made virtual.