ProgrammingVery hardGATE CS · Accenture

What is printed? (C++, assume <iostream> and using namespace std)

What is printed? (C++, assume <iostream> and using namespace std)
struct A { A() { cout << "A"; } };
struct B : virtual A { };
struct C : virtual A { };
struct D : B, C { };
int main() { D d; }
  1. A. AA
  2. B. A
  3. C. AAA
  4. D. Nothing

Correct answer: B. A

With virtual inheritance the most derived class constructs one shared A sub-object, so A's constructor runs once.

More OOP with C++ and Java questions

Exams that ask this topic