Constructors set up objects when they are created. Knowing the order helps you understand how parts of a program start working together.
Constructor calling order in C++
class Base { public: Base() { /* base constructor code */ } }; class Derived : public Base { public: Derived() { /* derived constructor code */ } };
The base class constructor runs before the derived class constructor.
Member objects are constructed before the body of the constructor runs, in the order they are declared.
class Base { public: Base() { std::cout << "Base constructor\n"; } }; class Derived : public Base { public: Derived() { std::cout << "Derived constructor\n"; } }; int main() { Derived d; return 0; }
class Member { public: Member() { std::cout << "Member constructor\n"; } }; class Container { Member m1; Member m2; public: Container() { std::cout << "Container constructor\n"; } }; int main() { Container c; return 0; }
This program shows the order of constructor calls: first the base class, then member objects in order, then the derived class.
#include <iostream> class Base { public: Base() { std::cout << "Base constructor\n"; } }; class Member { public: Member() { std::cout << "Member constructor\n"; } }; class Derived : public Base { Member m1; Member m2; public: Derived() { std::cout << "Derived constructor\n"; } }; int main() { Derived d; return 0; }
Base class constructors always run before derived class constructors.
Member objects are constructed in the order they are declared, not the order in the constructor initializer list.
Destructor calls happen in the reverse order of constructors.
Base class constructor runs first.
Member objects are constructed next, in declaration order.
Derived class constructor runs last.
Practice
Solution
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.Step 2: Identify the first constructor called
After the base class constructor, member objects and then the derived class constructor run.Final Answer:
Base class constructor -> Option CQuick Check:
Base constructor runs first [OK]
- Thinking derived constructor runs first
- Confusing member object construction order
- Assuming destructors run during construction
Solution
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.Step 2: Match correct syntax
Derived() : Base() {} uses the correct syntax: Derived() : Base() {}. Others are invalid or incorrect.Final Answer:
Derived() : Base() {} -> Option DQuick Check:
Use colon and parentheses for base constructor call [OK]
- Calling base constructor inside derived constructor body
- Using arrow (->) instead of colon
- Omitting parentheses after base class name
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?
Solution
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 ".Step 2: Combine outputs in order
The output is "A B C " exactly in that sequence.Final Answer:
A B C -> Option AQuick Check:
Base, members, derived = A B C [OK]
- Assuming members construct before base
- Mixing order of output strings
- Ignoring member object construction
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?
Solution
Step 1: Analyze object declaration
The line 'Derived d();' is a function declaration, not an object instantiation, so no constructor runs.Step 2: Understand program output
Since no object is created, no constructors run and no output is printed.Final Answer:
No output -> Option AQuick Check:
Function declaration, no object created [OK]
- Thinking 'Derived d();' creates an object
- Expecting constructor output
- Ignoring C++ parsing rules
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?
Solution
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.Step 2: Derived class constructor runs last
Finally, derived class Z constructor runs printing "Z ".Final Answer:
X Y Y Z -> Option BQuick Check:
Base, members in order, derived = X Y Y Z [OK]
- Mixing member construction order
- Assuming derived runs before members
- Ignoring multiple member objects
Ready to prove it?
Pass the test and this pattern moves unknown
