ProgrammingHardCapgemini · TCS NQT

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

What is printed? (C++, assume <iostream> and using namespace std)
struct M { M() { cout << "M"; } };
struct B { B() { cout << "B"; } };
struct D : B {
    M m;
    D() { cout << "D"; }
};
int main() { D d; }
  1. A. MBD
  2. B. DBM
  3. C. BMD
  4. D. BDM

Correct answer: C. BMD

Order: base class constructors, then member objects, then the derived constructor body.

More OOP with C++ and Java questions

Exams that ask this topic