ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
struct B {
    void f(int)    { cout << "Bi"; }
    void f(double) { cout << "Bd"; }
};
struct D : B {
    void f(double) { cout << "Dd"; }
};
int main() {
    D d;
    d.f(5);
}
  1. A. Bd
  2. B. Compilation error: ambiguous call
  3. C. Bi
  4. D. Dd

Correct answer: D. Dd

Declaring any f in D hides every f inherited from B (name hiding), so only D::f(double) is considered and 5 is converted to 5.0: Dd. Adding using B::f; inside D would bring B::f(int) back and print Bi.

More OOP with C++ and Java questions

Exams that ask this topic