Question:

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
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: The inheritance chain is A is the base, B derives from A, and C derives from B.

Step 2: When an object of the most derived class is created, C++ builds it from the top of the hierarchy down. Each class constructor first calls its base class constructor before running its own body.

Step 3: Creating
 C obj;
triggers C(). Before C() runs its body, it calls B(). Before B() runs its body, it calls A(). A has no base, so A() body runs first and prints A.

Step 4: Control returns to B() body, which prints B. Then control returns to C() body, which prints C.

So the output order is \( A \to B \to C \), that is A B C.

Answer: option A.
Was this answer helpful?
0
0