ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: B. Base Mixin1 Mixin2 Combo

Base classes always construct in the order they are declared in the class's inheritance list (Base, Mixin1, Mixin2), never in the order written in the member-initializer list; most compilers warn about the mismatched initializer-list order shown here but it still compiles this way.

More OOP with C++ and Java questions

Exams that ask this topic