ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct A { virtual void f() { cout << "A"; } };
struct B { virtual void f() { cout << "B"; } };
struct C : A, B { void f() override { cout << "C"; } };
int main() {
    C c;
    A& a = c; B& b = c;
    a.f(); b.f();
}
  1. A. AB
  2. B. CB
  3. C. Compile-time error: C::f overrides two functions
  4. D. CC

Correct answer: D. CC

One function in C with the matching signature overrides f in BOTH bases, so calls through either base reference dispatch to C::f.

More OOP with C++ and Java questions

Exams that ask this topic