ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

Correct answer: A. Base() Mixin1() Mixin2() Derived()

With multiple (non-virtual) base classes, construction order follows the order bases are listed in the class's own derivation list (Base, Mixin1, Mixin2), NOT the order of any initializer list, followed last by the derived class's own constructor body.

More OOP with C++ and Java questions

Exams that ask this topic