0
0
C++programming~10 mins

Runtime polymorphism in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Runtime polymorphism
Create base class pointer
Assign derived class object
Call virtual function
At runtime, decide which function to call
Execute derived class function if overridden
End
Runtime polymorphism lets a program decide which function to call during execution using base class pointers to derived objects.
Execution Sample
C++
#include <iostream>
using namespace std;
class Base {
public:
  virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
  void show() override { cout << "Derived"; }
};
int main() {
  Base* b = new Derived();
  b->show();
  delete b;
  return 0;
}
This code creates a base class pointer to a derived class object and calls a virtual function, showing runtime polymorphism.
Execution Table
StepActionPointer TypeObject TypeFunction CalledOutput
1Create Derived object-Derived--
2Assign Derived object to Base pointerBase*Derived--
3Call show() via Base pointerBase*DerivedDerived::show()Derived
4Program ends----
💡 Program ends after calling Derived::show() via Base pointer due to virtual function.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
b (Base*)nullptrnullptrpoints to Derived objectpoints to Derived objectpoints to Derived object
Key Moments - 2 Insights
Why does calling show() on a Base pointer call Derived's show()?
Because show() is declared virtual in Base, the program decides at runtime to call Derived's version, as shown in execution_table step 3.
What if show() was not virtual? Which function would be called?
If show() was not virtual, Base's show() would be called even if the pointer points to Derived, because function binding would happen at compile time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 3?
ABase
BDerived
CNo output
DCompilation error
💡 Hint
Check the 'Output' column in execution_table row for step 3.
At which step does the Base pointer start pointing to the Derived object?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Pointer Type' and 'Object Type' columns in execution_table.
If show() was not virtual, how would the function called at step 3 change?
ABase::show() would be called
BNo function would be called
CDerived::show() would still be called
DProgram would crash
💡 Hint
Refer to key_moments explanation about virtual keyword effect.
Concept Snapshot
Runtime polymorphism in C++ uses virtual functions.
Base class declares a function virtual.
Derived class overrides it.
Base class pointer can point to Derived object.
Calling virtual function via base pointer calls derived version at runtime.
Full Transcript
Runtime polymorphism allows a program to decide which function to call while running. In C++, this is done using virtual functions. A base class declares a function as virtual. A derived class overrides this function. When a base class pointer points to a derived class object and calls the virtual function, the derived class's version runs. This is shown in the example where a Base pointer points to a Derived object. Calling show() on the Base pointer calls Derived's show() because of the virtual keyword. If the function was not virtual, Base's version would run instead. This behavior is called dynamic dispatch and is key to runtime polymorphism.