ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
struct B { virtual void f() { cout << "B"; } };
struct D : B { void f() override { cout << "D"; } };
int main() { D d; B* p = &d; p->f(); p->B::f(); }
  1. A. DD
  2. B. DB
  3. C. BB
  4. D. BD

Correct answer: B. DB

A qualified call p->B::f() suppresses virtual dispatch and calls exactly B::f; the unqualified p->f() dispatches to D::f.

More OOP with C++ and Java questions

Exams that ask this topic