ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
struct A { A() { cout << "A"; } };
struct B : virtual A { B() { cout << "B"; } };
struct C : virtual A { C() { cout << "C"; } };
struct D : B, C { D() { cout << "D"; } };
int main() { D d; }
  1. A. ABACD
  2. B. DCBA
  3. C. ABCD
  4. D. BCAD

Correct answer: C. ABCD

With virtual inheritance D contains one shared A, constructed first by the most-derived class, then the bases in declaration order B, C, then D: ABCD. Without virtual each path would build its own A: ABACD.

More OOP with C++ and Java questions

Exams that ask this topic