ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
struct A { A() { cout << "A"; } };
struct B { B() { cout << "B"; } };
struct C : A, B { C() { cout << "C"; } };
int main() { C c; }- A. BAC
- B. ABC
- C. CAB
- D. Compile-time error: C must explicitly initialize both A and B
Correct answer: B. ABC
With multiple (non-virtual) inheritance, base classes construct strictly in the order they appear in the inheritance list: A, then B, before C's own body runs. No explicit initializer-list entries are needed since both have usable default constructors; the implicit calls still follow declaration order, giving ABC.
More OOP with C++ and Java questions
- What is printed? cpp struct Animal { virtual std::string sound() const { return "..."; } }; struct Dog : Animal { std::string sound()…
- What is printed? cpp struct Shape { virtual double area() const = 0; void report() const { std::cout << "Area: " << area(); } }; struct…
- What is the behavior? cpp struct Base { ~Base() { std::cout << "~Base "; } // NOT virtual }; struct Derived : Base { int data = new…
- What is printed? cpp struct A { virtual void f(int x) { std::cout << "A::f(int) " << x; } }; struct B : A { virtual void f(double x) {…
- What is printed? cpp struct Base { virtual void f() const { std::cout << "Base::f const"; } }; struct Derived : Base { void f() {…
- What is printed? cpp struct IWalk { virtual void move() = 0; }; struct ISwim { virtual void move() = 0; }; struct Duck : IWalk, ISwim {…