ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct V { V() { cout << 'V'; } };
struct X { X() { cout << 'X'; } };
struct M { M() { cout << 'm'; } };
struct D : X, virtual V {
    M m;
    D() { cout << 'D'; }
};
int main() { D d; }
  1. A. XVmD
  2. B. mXVD
  3. C. VXmD
  4. D. XmVD

Correct answer: C. VXmD

Virtual bases are constructed before all non-virtual bases, regardless of the order in the base list. Then the non-virtual base X, then members (m), and finally the constructor body.

More OOP with C++ and Java questions

Exams that ask this topic