We know that runtime polymorphism is achieved when the objects of different classes in the class hierarchy respond to the same function call each in its way. To invoke same-named functions present both in the base and derived classes using a single interface. But we found that it still could not execute the derived class’s functions and executed the same-named function defined in the base class. Thus it could not achieve polymorphism. To implement polymorphism for invoking the exact version of the same-named member functions in the class hierarchy, we use virtual functions.
A virtual function is a member function declared in the base class using the keyword virtual whose functionality is redefined (same function name) by its derived classes. To declare the virtual function in the base class, precede its function declaration with a keyword virtual. Virtual functions employ late binding by allocating memory space during execution time and not during compilation time. In this case when a pointer of the base class is defined in a main() function and derived class object’s address is passed on to the base class pointer, then calling the overridden function will invoke the derived class member function and not the base class member function as mentioned earlier.
To implement run time polymorphism using the virtual function, it must be invoked through the base class pointer that can contain objects of different derived classes. When the virtual function is invoked through the base class pointer, the compiler chooses the appropriate member function of the derived class at run time depending upon the base class pointer contents and not the type of pointer. Thus by making the base class pointer pointing to different classes’ objects, the different versions of virtual functions can be called at run time. Hence, run time polymorphism is implemented.
Virtual functions are named so because when virtual functions are used in the class hierarchy, the programmer appears to call a function defined in the base class (common interface) but may, in reality, be calling a function of its derived class.
To understand how virtual function works. The complete program that implements runtime polymorphism using the virtual function is as shown.
#include<iostream.h> #include<conio.h> class base { public: virtual void display() //virtual function {cout<<"Base class display is called\n"; } }; class derv1: public base { public: void display() {cout<<"Derv1' s display called\n"; } }; class derv2: public base { public: void display () { cout<<"Derv2's display called\n"; } }; int main() { clrscr(); base *ptr; derv1 d1; derv2 d2; ptr=&d1; ptr->display(); ptr=&d2 ptr->display() ; getch() ; return 0; } Output: Dervl's display called Derv2's display called
Explanation: This program’s output reveals that the member function display() of the derived classes are invoked and not that of the base class as expected. By defining the member function display () as virtual in the base class,
virtual void display(){ ... )
every call to this function through the base class pointer (ptr) will be dynamically bound (run time) and not compile. So if the base class pointer ptr contains the address of the object d1 of the derv1 class, then the statement.
ptr->display();
invokes the member function defined in the derived class derv1. The compiler chooses the appropriate member function of derived class derv1 at run time depending upon the base class pointer contents and not the type of pointer. Here the compiler does not perform static binding as addresses of the derived class objects are not known to the base class pointer at compile time which is available only at run time. Hence binding decision is postponed until run time. Similarly, if the base class pointer ptr contains the address of object d2 of derv2 class, then the statement ptr->display(); invokes the display() function defined in the derv2 class.
Thus, we see that the same function call.
prt->display();
executes a different function based on the contents of ptr and hence implements run-time polymorphism.