Bird
Raised Fist0
C++programming~10 mins

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

Choose your learning style10 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'.

Practice

(1/5)
1. In C++, when creating an object of a derived class, which constructor is called first?
easy
A. Member objects' constructors
B. Derived class constructor
C. Base class constructor
D. Destructor of the base class

Solution

  1. Step 1: Understand constructor order in inheritance

    When an object of a derived class is created, the base class constructor runs first to initialize the base part.
  2. Step 2: Identify the first constructor called

    After the base class constructor, member objects and then the derived class constructor run.
  3. Final Answer:

    Base class constructor -> Option C
  4. Quick Check:

    Base constructor runs first [OK]
Hint: Base constructor always runs before derived [OK]
Common Mistakes:
  • Thinking derived constructor runs first
  • Confusing member object construction order
  • Assuming destructors run during construction
2. Which of the following is the correct syntax to call a base class constructor from a derived class constructor in C++?
easy
A. Derived() { Base(); }
B. Derived() : Base {}
C. Derived() -> Base() {}
D. Derived() : Base() {}

Solution

  1. Step 1: Recall base constructor call syntax

    In C++, the base class constructor is called in the initializer list of the derived constructor using a colon and parentheses.
  2. Step 2: Match correct syntax

    Derived() : Base() {} uses the correct syntax: Derived() : Base() {}. Others are invalid or incorrect.
  3. Final Answer:

    Derived() : Base() {} -> Option D
  4. Quick Check:

    Use colon and parentheses for base constructor call [OK]
Hint: Use colon and parentheses to call base constructor [OK]
Common Mistakes:
  • Calling base constructor inside derived constructor body
  • Using arrow (->) instead of colon
  • Omitting parentheses after base class name
3. Consider the following code:
class A {
public:
  A() { std::cout << "A "; }
};
class B {
public:
  B() { std::cout << "B "; }
};
class C : public A {
  B b;
public:
  C() { std::cout << "C "; }
};

int main() {
  C obj;
  return 0;
}

What is the output when this program runs?
medium
A. A B C
B. B A C
C. C A B
D. A C B

Solution

  1. Step 1: Determine constructor call order

    First, base class A constructor runs printing "A ". Then member object b of class B is constructed printing "B ". Finally, derived class C constructor runs printing "C ".
  2. Step 2: Combine outputs in order

    The output is "A B C " exactly in that sequence.
  3. Final Answer:

    A B C -> Option A
  4. Quick Check:

    Base, members, derived = A B C [OK]
Hint: Base then members then derived constructor order [OK]
Common Mistakes:
  • Assuming members construct before base
  • Mixing order of output strings
  • Ignoring member object construction
4. Given this code snippet:
class Base {
public:
  Base() { std::cout << "Base "; }
};
class Derived : public Base {
public:
  Derived() { std::cout << "Derived "; }
};

int main() {
  Derived d();
  return 0;
}

What is the output when this program runs?
medium
A. No output
B. Derived Base
C. Base Derived
D. Compilation error

Solution

  1. Step 1: Analyze object declaration

    The line 'Derived d();' is a function declaration, not an object instantiation, so no constructor runs.
  2. Step 2: Understand program output

    Since no object is created, no constructors run and no output is printed.
  3. Final Answer:

    No output -> Option A
  4. Quick Check:

    Function declaration, no object created [OK]
Hint: Watch out for most vexing parse: 'Derived d();' is a function [OK]
Common Mistakes:
  • Thinking 'Derived d();' creates an object
  • Expecting constructor output
  • Ignoring C++ parsing rules
5. You have a class hierarchy:
class X {
public:
  X() { std::cout << "X "; }
};
class Y {
public:
  Y() { std::cout << "Y "; }
};
class Z : public X {
  Y y1, y2;
public:
  Z() { std::cout << "Z "; }
};

int main() {
  Z obj;
  return 0;
}

What is the exact output when this program runs?
hard
A. Y Y X Z
B. X Y Y Z
C. X Z Y Y
D. Y X Y Z

Solution

  1. Step 1: Identify constructor call order

    First, base class X constructor runs printing "X ". Then member objects y1 and y2 of class Y are constructed in declaration order, printing "Y " twice.
  2. Step 2: Derived class constructor runs last

    Finally, derived class Z constructor runs printing "Z ".
  3. Final Answer:

    X Y Y Z -> Option B
  4. Quick Check:

    Base, members in order, derived = X Y Y Z [OK]
Hint: Members construct in declaration order after base [OK]
Common Mistakes:
  • Mixing member construction order
  • Assuming derived runs before members
  • Ignoring multiple member objects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard