Topic 8

Runtime Polymorphism (Virtual Functions & Dynamic Binding)

3 min read3 questions to test yourself

1Definition

Runtime Polymorphism (also called Dynamic Polymorphism or Late Binding) is a mechanism where a call to an overridden member function is resolved at runtime based on the actual type of the object referenced by a base class pointer.

2Simple Explanation

In runtime polymorphism, a base class pointer can point to any derived class object. When a method marked with the virtual keyword is called using that pointer, C++ dynamically executes the derived class's version of the function rather than the base class's version.

3Real-Life Example / Analogy

  • Person's Role depending on Context: A single person behaves differently depending on their location:
    • At School →\rightarrow behaves like a Student.
    • At Home →\rightarrow behaves like a Son/Daughter.
    • In a Shopping Mall →\rightarrow behaves like a Customer.
    • On a Bus →\rightarrow behaves like a Passenger. The person's core interface is identical, but their active behavior changes dynamically depending on the current environment.

4Diagram / Visual Representation

Base pointer
Base* bptr = &derivedObj

Derived object

Virtual dispatch

Virtual table (vtable)
Derived::show() entry

Call resolves at runtime

Executes Derived::show()

5Working / How It Works

  1. When a class contains a virtual function, the compiler creates a hidden lookup array called a VTABLE (Virtual Table) for that class.
  2. Every object of that class contains a hidden pointer called vptr (Virtual Pointer) pointing to the VTABLE.
  3. During runtime execution of bptr->show(), the program follows vptr →\rightarrow inspects VTABLE →\rightarrow dispatches the derived class method address.

6Types / Components / Characteristics

  • Key Concepts:
    • Function Overriding: Child class re-defines a base class virtual method using the exact same signature and return type.
    • Virtual Keyword: Enables dynamic late binding.
    • Virtual Destructor: Ensures derived class destructors run when deleting objects via base pointers.
  • Compile-Time vs. Runtime Polymorphism:
Feature Compile-Time Polymorphism Runtime Polymorphism
Mechanism Function / Operator Overloading Function Overriding via Virtual Functions
Binding Early Static Binding Late Dynamic Binding
Execution Speed Faster Slightly slower (VTABLE lookup overhead)
Pointer Required No pointers required Requires Base Class Pointer or Reference

7Solved Examples / Problems

Problem Statement: Write a C++ program demonstrating virtual functions and runtime function overriding.

C++
#include <iostream>
using namespace std;
class Base {
public:
    virtual void show() { // Virtual function
        cout << "Base Class Show" << endl;
    }
};
class Derived : public Base {
public:
    void show() override { // Overridden function
        cout << "Derived Class Show" << endl;
    }
};
int main() {
    Base* bptr;
    Derived obj;
    bptr = &obj;
    // Late binding resolves to Derived::show()
    bptr->show();
    return 0;
}

Output:

Output
Derived Class Show

8Advantages, Disadvantages & Applications

  • Advantages:
    • Enables extensible frameworks where new derived classes can be added without modifying existing caller code.
  • Disadvantages:
    • Slight memory footprint increase per object (due to vptr).
    • Minimal execution latency penalty during VTABLE pointer dereferencing.
  • Applications: Game engine entity components, graphics rendering engines (Shape* s = new Circle()).

9Important Points to Remember

  • Virtual functions cannot be static.
  • Constructors cannot be virtual, but destructors should be virtual when working with derived class hierarchies.

10Test Your Understanding

  1. Conceptual Question: Why should a base class destructor be declared virtual when derived objects are deleted via base pointers?
    Show answer
    To ensure that the derived class destructor is invoked first, freeing child resources before the base destructor runs, preventing memory leaks.
  2. MCQ: What internal data structure does C++ use behind the scenes to support virtual functions?
    • A) Stack Frame Array
    • B) VTABLE (Virtual Table)
    • C) Heap Allocation Table
    • D) Hash Map
    Show answer
    B) VTABLE (Virtual Table)
  3. Application Question: What is printed if virtual is removed from virtual void show() in the base class?
    Show answer
    "Base Class Show", because early static binding binds the call to the base pointer type.

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic