ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
struct A { A() { cout << "A"; } };
struct B { B() { cout << "B"; } };
struct C : A, B { C() { cout << "C"; } };
int main() { C c; }
  1. A. BAC
  2. B. ABC
  3. C. CAB
  4. D. Compile-time error: C must explicitly initialize both A and B

Correct answer: B. ABC

With multiple (non-virtual) inheritance, base classes construct strictly in the order they appear in the inheritance list: A, then B, before C's own body runs. No explicit initializer-list entries are needed since both have usable default constructors; the implicit calls still follow declaration order, giving ABC.

More OOP with C++ and Java questions

Exams that ask this topic