0
0
C++programming~10 mins

Constructor calling order in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constructor calling order
Create Derived object
Call Base class constructor
Call Member objects constructors
Call Derived class constructor
Object fully constructed
When creating an object of a derived class, first the base class constructor runs, then member objects constructors, and finally the derived class constructor.
Execution Sample
C++
#include <iostream>

class Base {
public:
  Base() { std::cout << "Base\n"; }
};
class Derived : public Base {
public:
  Derived() { std::cout << "Derived\n"; }
};
int main() {
  Derived d;
}
This code creates a Derived object, showing the order in which constructors are called.
Execution Table
StepActionOutputExplanation
1Derived object creation startsProgram starts creating Derived object
2Base constructor calledBaseBase class constructor runs first
3Derived constructor calledDerivedDerived class constructor runs after Base
4Object fully constructedConstruction complete
💡 Derived object is fully constructed after Base and Derived constructors run
Variable Tracker
VariableStartAfter Base ctorAfter Derived ctorFinal
Derived objectNot createdBase part constructedFully constructedFully constructed
Key Moments - 2 Insights
Why does the Base constructor run before the Derived constructor?
Because the base class part must be ready before the derived class can add its own initialization, as shown in execution_table step 2 and 3.
What if the Derived class has member objects? When are their constructors called?
Member objects are constructed after the base class constructor but before the derived class constructor, which is not shown here but is important to remember.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 2?
ABase
BDerived
CNothing
DError
💡 Hint
Check the 'Output' column in execution_table row with Step 2
At which step does the Derived constructor run?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when Derived constructor is called
If the Base constructor was missing, what would happen to the order?
AMember objects constructors run first
BCompilation error
CDerived constructor runs first
DBase constructor is called automatically
💡 Hint
Remember base class constructor must exist or be defaulted; see key_moments about constructor order
Concept Snapshot
Constructor calling order in C++:
- Base class constructor runs first
- Then member objects constructors
- Finally derived class constructor
This ensures proper initialization from base to derived.
Full Transcript
When you create an object of a derived class in C++, the program first calls the base class constructor. This sets up the base part of the object. Next, constructors for any member objects inside the derived class run. Finally, the derived class constructor runs to finish the object setup. This order is important to make sure everything is ready before the derived class adds its own details. In the example, creating a Derived object prints 'Base' first, then 'Derived'.