ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct A { A() { cout << "A"; } ~A() { cout << "a"; } };
struct B { B() { cout << "B"; } ~B() { cout << "b"; } };
struct M { M() { cout << "M"; } ~M() { cout << "m"; } };
struct C : B, A {
    M m;
    C()  { cout << "C"; }
    ~C() { cout << "c"; }
};
int main() { C c; }
  1. A. ABMCcmba
  2. B. BAMCcmab
  3. C. BAMCcbam
  4. D. MBACcabm

Correct answer: B. BAMCcmab

Bases are constructed in the order they are LISTED in the class head (B, then A), then members, then the constructor body. Destruction is the exact reverse: body, member, A, B.

More OOP with C++ and Java questions

Exams that ask this topic