ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct A { void hi()    { cout << "A"; } };
struct B { void hi(int) { cout << "B"; } };
struct C : A, B { };
int main() { C c; c.hi(1); }
  1. A. Compile-time error: the name hi is ambiguous
  2. B. B
  3. C. A
  4. D. AB

Correct answer: A. Compile-time error: the name hi is ambiguous

Name lookup happens before overload resolution: hi is found in two different base classes, so the lookup itself is ambiguous even though only one signature would match. Adding using A::hi; using B::hi; in C fixes it.

More OOP with C++ and Java questions

Exams that ask this topic