Inheritance/Polymorphism  «Prev  Next»
Lesson 3 Virtual member functions and pure polymorphism
Objective Virtual member functions and how they are used.

C++ Pure Polymorphism

In C++, polymorphism is a principal feature of object-oriented programming that allows objects of different types to be treated as objects of a common type, enhancing flexibility and increasing code maintainability and reusability. One way C++ achieves polymorphism is through the use of virtual member functions.
A virtual member function is a function declared as 'virtual' in a base class and meant to be overridden in a derived class. When a function is declared as 'virtual' in the base class, the function call is resolved at runtime based on the type of the object pointed to or referred to, rather than the type of the pointer or reference. This is called runtime or dynamic polymorphism.
Here's the general form of a virtual function:
virtual return_type function_name (argument list)

Here is an example that demonstrates polymorphism using virtual functions:
class Base {
   virtual void show() { cout<<" In Base \n"; }

class Derived: public Base {
   void show() { cout<< "In Derived \n"; }

int main(void) {
   Base *bp = new Derived;
   bp->show();  // prints "In Derived"
   return 0;

Virtual Member Functions

C++ supports virtual member functions. These are functions declared in the base class and redefined in a derived class. When a virtual member function is redefined, it is overridden as opposed to overloaded. We will look at the difference between overriding and overloading later in this module. For now, just think of the difference as semantic.
  1. Virtual member functions can be overridden, and
  2. nonvirtual member functions can be overloaded.
A class hierarchy that is defined by inheritance creates a related set of user-defined types, all of whose objects may be pointed at by a base class pointer. By accessing the virtual function through this pointer, C++ selects the appropriate function definition at runtime.
This is a form of polymorphism called pure polymorphism.

OOP Design Methodology

Inheritance should be designed into software to maximize reuse and to allow a natural modeling of the problem domain.
The key elements of the OOP design methodology are as follows:
  1. Decide on an appropriate set of types.
  2. Design in their relatedness, and use inheritance to share code.
  3. Use virtual functions to process related objects polymorphically.


Polymorphism is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type or types. There are several fundamentally different kinds of polymorphism. If a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations, it is called ad hoc polymorphism. Ad hoc polymorphism[1] is supported in many languages using function overloading.

Polymorphism allows you to take advantage of the commonality between related classes, while still giving each class the flexibility to implement specific behavior. Using polymorphism, it is possible to build very flexible and extensible systems. Polymorphism (literally, having multiple shapes) describes a set of objects of different classes with similar behavior.
[1]ad hoc polymorphism: In programming languages, ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types. A polymorphic function can denote a number of distinct implementations depending on the type of arguments to which it is applied.