ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct B {
    void f(int)    { cout << "Bi"; }
    void f(double) { cout << "Bd"; }
};
struct D : B {
    using B::f;
    void f(double) { cout << "Dd"; }
};
int main() {
    D d;
    d.f(3);
    d.f(3.0);
}
  1. A. DdDd
  2. B. BiBd
  3. C. BiDd
  4. D. Compile error: ambiguous f(double)

Correct answer: C. BiDd

The using-declaration brings B's overloads into D, except that a B member with the same signature as one declared in D is hidden by D's. So f(3) picks B::f(int) and f(3.0) picks D::f(double), with no ambiguity.

More OOP with C++ and Java questions

Exams that ask this topic