0
0
C++programming~10 mins

Virtual functions in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual functions
Create Base class pointer
Assign Derived object to pointer
Call virtual function
Check object's actual type
Call Derived's override if exists
Return result
A base class pointer calls a virtual function, which runs the derived class's version if overridden.
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* ptr;
  ptr = new Derived();
  ptr->show();
  delete ptr;
  return 0;
}
Shows how a base class pointer calls the derived class's overridden virtual function.
Execution Table
StepActionPointer TypeObject TypeFunction CalledOutput
1Create Base pointerBase*NoneNone
2Assign Derived object to pointerBase*DerivedNone
3Call show() via pointerBase*DerivedDerived::show()Derived
4End of execution----
💡 Function call resolved to Derived::show() because show() is virtual and object is Derived
Variable Tracker
VariableStartAfter Step 2After Step 3Final
ptrnullpoints to Derived objectpoints to Derived objectpoints to Derived object
Key Moments - 2 Insights
Why does ptr->show() call Derived::show() even though ptr is a Base*?
Because show() is declared virtual in Base, the program checks the actual object type (Derived) at runtime and calls Derived's version (see execution_table step 3).
What if show() was not virtual? Which function would be called?
If show() was not virtual, Base::show() would be called regardless of the object type, because function calls are resolved by pointer type at compile time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 3?
ANo output
BBase
CDerived
DCompilation error
💡 Hint
Check the 'Output' column in execution_table row for step 3
At which step does the pointer start pointing to a Derived object?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Pointer Type' and 'Object Type' columns in execution_table
If show() was not virtual, what would be the function called at step 3?
ADerived::show()
BBase::show()
CNo function called
DCompiler error
💡 Hint
Refer to key_moments explanation about virtual vs non-virtual function calls
Concept Snapshot
Virtual functions allow a base class pointer to call derived class methods at runtime.
Declare a function with 'virtual' in base class.
Override it in derived class with same signature.
Calls via base pointer use derived version if overridden.
Enables runtime polymorphism in C++.
Full Transcript
This example shows how virtual functions work in C++. We create a base class pointer and assign it a derived class object. When we call the virtual function show() through the base pointer, the program checks the actual object type at runtime. Because the object is of the derived class, the derived class's show() function runs, printing 'Derived'. This is different from normal functions, which are decided at compile time based on pointer type. Virtual functions enable this dynamic behavior, called runtime polymorphism.