ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
struct Base { Base(){ std::cout << "Base "; } };
struct Mem { Mem(){ std::cout << "Mem "; } };
struct Derived : Base {
    Mem m;
    Derived(){ std::cout << "Derived "; }
};
int main(){ Derived d; }
  1. A. Mem Base Derived
  2. B. Derived Base Mem
  3. C. Base Mem Derived
  4. D. Base Derived Mem

Correct answer: C. Base Mem Derived

Base class sub-objects always construct before members, and members construct before the derived constructor's own body runs, regardless of how or whether they appear in the initializer list.

More OOP with C++ and Java questions

Exams that ask this topic