ProgrammingVery hardTCS NQT · GATE CS

What is printed? (C++, assume <iostream> and using namespace std)

What is printed? (C++, assume <iostream> and using namespace std)
struct B { void f(int)    { cout << "Bi"; } };
struct D : B { void f(double) { cout << "Dd"; } };
int main() { D d; d.f(1); }
  1. A. Bi
  2. B. Dd
  3. C. BiDd
  4. D. Compile error due to ambiguity

Correct answer: B. Dd

D::f hides every B::f by name, so overload resolution only sees f(double) and 1 is converted to double.

More OOP with C++ and Java questions

Exams that ask this topic