ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
struct B { B(int x) { cout << x; } };
struct D : B { using B::B; };
int main() { D d(5); }
  1. A. Compile-time error: D has no constructor taking int
  2. B. Nothing is printed
  3. C. 5
  4. D. 50

Correct answer: C. 5

using B::B; (inheriting constructors, C++11) makes B's constructors usable for D, so D d(5) calls B(5). Without it, D would have no suitable constructor.

More OOP with C++ and Java questions

Exams that ask this topic