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); }- A. Compile-time error: the name hi is ambiguous
- B. B
- C. A
- 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
- What is printed? cpp struct A { virtual void f() { cout << "A"; } }; struct B { virtual void f() { cout << "B"; } }; struct C : A, B {…
- How many of these statements about C++ are correct? (i) A constructor cannot be declared virtual. (ii) A static member function cannot be…
- What is printed? cpp template <class T> void f(T) { cout << "1"; } template <> void f(int) { cout << "2"; } // explicit specialisation…
- What is printed? cpp template <int N> struct F { static const int v = N F<N - 1>::v; }; template <> struct F<0> { static const int v = 1…
- What is printed? cpp struct M { M() { throw 1; } }; struct S { M m; S() try : m() { } catch (int) { cout << "S"; } }; int main() { try { S…
- What is printed? cpp void f() noexcept { } void g() { } int main() { cout << noexcept(f()) << noexcept(g()); }