ProgrammingVery hardTCS NQT Prime · Infosys SP

What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std)

What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { void f(int) { cout << "Ai"; } void f(double) { cout << "Ad"; } };
struct B : A { void f(char*) { cout << "Bs"; } };
int main() { B b; b.f(1.5); }
  1. A. Compile error
  2. B. Ad
  3. C. Ai
  4. D. Bs

Correct answer: A. Compile error

B::f hides every A::f by name, so only f(char*) is visible and a double cannot convert to char*: compile error (a using-declaration would be needed).

More OOP with C++ and Java questions

Exams that ask this topic