ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

What is printed?
struct B {
    void f(int)         { cout << "Bi"; }
    void f(const char*) { cout << "Bs"; }
};
struct D1 : B { void f(double) { cout << "Dd"; } };
struct D2 : B { using B::f; void f(double) { cout << "Dd"; } };
int main() {
    D1 x; D2 y;
    x.f(1);
    y.f(1);
    y.f(2.5);
}
  1. A. BiBiDd
  2. B. DdBiDd
  3. C. DdDdDd
  4. D. BiDdDd

Correct answer: B. DdBiDd

Declaring f in D1 hides every B::f, so x.f(1) can only convert 1 to double and calls D1::f. The using-declaration in D2 brings B's overloads into the same set, so y.f(1) picks the exact match B::f(int), and y.f(2.5) the exact D2::f(double).

More OOP with C++ and Java questions

Exams that ask this topic