ProgrammingVery hardGATE CS · TCS Digital · Advanced

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

What is printed? (C++17, assume <iostream> and using namespace std)
struct B {
    void f(int)    { cout << "Bi"; }
    void f(double) { cout << "Bd"; }
};
struct D : B {
    using B::f;
    void f(char)   { cout << "Dc"; }
};
int main() {
    D d;
    d.f(65);
    d.f(2.5f);
    d.f('a');
}
  1. A. BiBdBi
  2. B. BiDcDc
  3. C. BiBdDc
  4. D. DcDcDc

Correct answer: C. BiBdDc

The using-declaration puts B::f(int), B::f(double) and D::f(char) in one overload set. 65 is an exact int match; float to double is a promotion (better than the conversions to int or char); 'a' is an exact char match.

More OOP with C++ and Java questions

Exams that ask this topic