Dynamic polymorphism allows programmers to write code using functions applied to objects of the base class without worrying about how those functions will actually be defined by derived classes. For instance, programmers might call functions on a Shape object that computes its area or perimeter, without concerning themselves with the actual (derived) type of the object, whether it is a Rectangle, Square or Circle.

An essential requirement to take advantage of polymorphism in C++ is that there should be a way by which it is possible to refer to objects of derived classes using a pointer to a single type. This is possible by using a pointer to the base type, which can also point to derived class objects.

Whenever a derived class redefines a function in the base class, the function needs to be declared “virtual” by prefixing the keyword “virtual” to the normal function declaration in the base class. This should be done if the programmer wishes to take advantage of run-time polymorphism.

Overriding without the Virtual Keyword

Let us consider an example where the base-class function declaration is not preceded by the keyword virtual.

.cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: green; }
.cb2 { color: blue; }
.cb3 { color: maroon; }

 

/*
 * Program to illustrate function overriding without
 * virtual functions.
 */
#include<iostream>
using namespace std;
class B // Base Class
{
public:
 void Display()
 {
 cout << "Inside B::Display()" << endl;
 }
};

class D1 : public B // First derived class
{
public:
 void Display()
 {
 cout << "Inside D1::Display()" << endl ;
 }
};

class D2 : public B // Second derived class
{
public:
 void Display()
 {
 cout << "Inside D2::Display()" << endl ;
 }
};

int main()
{
 B* base_ptr ; // pointer to the object of the base class B
 D1 der1_obj ; // object of the first derived class D1
 base_ptr =&der1_obj ;
 base_ptr->Display(); // Accessing the member function Display()
 D2 der2_obj ; // object of the second derived class D2
 base_ptr = &der2_obj ;
 base_ptr->Display(); // Accessing the member function Display()

 return 0;
}

 

Output

Inside B::Display() Inside B::Display()

In this example, when the pointer to the base class tries to invoke the overridden function in the derived class object, the derived class function is not invoked. Instead, only the base-class member function is executed.

If the programmer writes virtual before the function declaration, the program determines, at run-time, which function to call based on the type of object pointed to by the base pointer, rather than the type of the pointer.

Example using Virtual Functions

The example given below illustrates the concept of polymorphism and virtual functions.

 

/*
 * This program illustrates the use of
 * virtual functions and polymorphism
 */

#include<iostream>
using namespace std;
class B // Base class
{
public:
 virtual void Display() // virtual function
 {
 cout << "Inside B::Display()" << endl ;
 }
};
class D1 : public B // First derived class
{
public:
 void Display()
 {
 cout << "Inside D1::Display()" << endl ;
 }
};
class D2 : public B // Second derived class
{
public:
 void Display()
 {
 cout << "Inside D2::Display()" << endl ;
 }
};

int main()
{
 B* base_ptr ; // Pointer to the object of the base class
 D1 der1_obj ;  

 base_ptr = &der1_obj; // The address of the object der1_obj
 // of the first derived class
 // D1 is assigned to the pointer
 // base_ptr to the base class B

 base_ptr->Display(); // Accessing the member function Display()

 D2 der2_obj; // Object of the second derived class D2

 base_ptr = &der2_obj; // The address of the object der2_obj
 // of the second derived class
 // D2 is assigned to the pointer
 // base_ptr of the base class B

 base_ptr->Display(); // Accessing the member function Display()

 return 0;
}

 

Output

Inside D1::Display() Inside D2::Display()