Question:easy

Given the following C++ code, what will be the order of constructor execution?

class A { public: A() { std::cout << "A" ; } };
class B : public A { public: B() { std::cout << "B" ; } };
class C : public B { public: C() { std::cout << "C" ; } };
int main() { C obj; return 0; }

Show Hint

Constructors run base class first. Which class sits at the very bottom of the chain with no parent?
Updated On: Jul 2, 2026
  • A B C
  • A C B
  • B C A
  • C A B
Show Solution

The Correct Option is A

Solution and Explanation

Different angle, base first rule:

The rule is simple. A constructor cannot finish its own work until its base sub object is fully built. So construction runs base first, then derived.

Line up the classes by depth:
$A$ at depth 0, $B$ at depth 1, $C$ at depth 2.

Building $C$ forces $B$ to be built, which forces $A$ to be built. The deepest base finishes first, then each layer above prints as control unwinds back up.

Print sequence:
\[ A \;\text{then}\; B \;\text{then}\; C \]
\[\boxed{\text{A B C (option A)}}\]
Was this answer helpful?
0