ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
struct A { A() { std::cout << "A "; } };
struct B { B() { std::cout << "B "; } };
struct C : A, B {
    C() { std::cout << "C "; }
};
int main() { C c; }
  1. A. B A C
  2. B. C A B
  3. C. A C B
  4. D. A B C

Correct answer: D. A B C

With multiple inheritance, base classes construct in the order they are LISTED in the derived class's inheritance list (A, B), not the order their constructors happen to appear or anything related to the member-initializer list's order. So A constructs, then B, then C's own body.

More OOP with C++ and Java questions

Exams that ask this topic