0
0
C++programming~10 mins

Base class pointers in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Base class pointers
Create Derived object
Assign Derived object address to Base pointer
Use Base pointer to access members
Call functions via Base pointer
If virtual, Derived version runs
If not virtual, Base version runs
A pointer of base class type can hold address of derived class object. Calling functions depends on virtual keyword.
Execution Sample
C++
#include <iostream>

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

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

int main() {
  Derived d;
  Base* ptr = &d;
  ptr->show();
  return 0;
}
Shows how a base class pointer calls a function when pointing to a derived object.
Execution Table
StepActionPointer TypeObject Pointed ToFunction CalledOutput
1Create Derived object d-Derived object d--
2Assign address of d to Base pointer ptrBase*Derived object d--
3Call ptr->show()Base*Derived object dBase::show()Base show
4Program ends----
💡 Function called depends on pointer type and virtual keyword; here show() is non-virtual, so Base::show() runs
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
duninitializedDerived object createdDerived object createdDerived object createdDerived object created
ptruninitializeduninitializedpoints to d (Derived)points to d (Derived)points to d (Derived)
Key Moments - 2 Insights
Why does ptr->show() call Base::show() and not Derived::show()?
Because show() is not declared virtual in Base, the function called depends on pointer type, not object type. See execution_table step 3.
Can a Base class pointer hold address of a Derived class object?
Yes, a Base class pointer can point to a Derived object. This is shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function is called at step 3?
ADerived::show()
BBase::show()
CNo function called
DCompiler error
💡 Hint
Check the 'Function Called' column at step 3 in execution_table.
At which step does the Base pointer start pointing to the Derived object?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Pointer Type' and 'Object Pointed To' columns in execution_table.
If show() was declared virtual in Base, what would ptr->show() call at step 3?
ABase::show()
BNo function called
CDerived::show()
DCompiler error
💡 Hint
Virtual functions call the derived version when accessed via base pointer.
Concept Snapshot
Base class pointers can hold addresses of derived objects.
Calling functions via base pointer calls base version unless function is virtual.
Virtual functions enable polymorphism: derived version runs.
Non-virtual functions call base version regardless of object type.
Use base pointers for flexible code with inheritance.
Full Transcript
This example shows how a pointer of base class type can point to a derived class object. We create a Derived object d, then assign its address to a Base class pointer ptr. When we call ptr->show(), since show() is not virtual, the Base class version runs, printing 'Base show'. If show() were virtual, the Derived version would run instead. This demonstrates how base class pointers work and how virtual functions enable polymorphism.