ProgrammingVery hardGATE CS · TCS Digital · Advanced

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

What is printed? (C++17, assume <iostream> and using namespace std)
struct V { V() { cout << "V"; } };
struct A : virtual V { A() { cout << "A"; } };
struct B : virtual V { B() { cout << "B"; } };
struct C : A, B {
    V m;
    C() { cout << "C"; }
};
int main() { C c; }
  1. A. VABC
  2. B. VAVBVC
  3. C. VABCV
  4. D. VABVC

Correct answer: D. VABVC

The virtual base V is built first and only once (by the most derived class), then A and B in declaration order, then C's own member m (another V), and finally C's body.

More OOP with C++ and Java questions

Exams that ask this topic