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 behaves like a Student.
- At Home behaves like a Son/Daughter.
- In a Shopping Mall behaves like a Customer.
- On a Bus 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
5Working / How It Works
- When a class contains a
virtualfunction, the compiler creates a hidden lookup array called a VTABLE (Virtual Table) for that class. - Every object of that class contains a hidden pointer called vptr (Virtual Pointer) pointing to the VTABLE.
- During runtime execution of
bptr->show(), the program followsvptrinspects VTABLE 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.
#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:
Derived Class Show8Advantages, 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.
- Slight memory footprint increase per object (due to
- 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
- Conceptual Question: Why should a base class destructor be declared
virtualwhen 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. - 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) - Application Question: What is printed if
virtualis removed fromvirtual void show()in the base class?Show answer
"Base Class Show", because early static binding binds the call to the base pointer type.
