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); }- A. Compile-time error: D has no constructor taking int
- B. Nothing is printed
- C. 5
- 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
- What is printed? cpp struct T { static int n; int id; T() : id(++n) { cout << id; } ~T() { cout << id; } }; int T::n = 0; int main() { T…
- What is printed? cpp int main() { int x = 1; auto byVal = [x] { return x; }; auto byRef = [&x] { return x; }; x = 5; cout << byVal() <<…
- Base b points to an object that is really a Base (not a Derived). What does staticcast<Derived>(b) do?
- What is printed? cpp struct S { int a = 1; int b = 2; S() : a(10) { } S(int) { } }; int main() { S x; S y(0); cout << x.a << x.b << y.a; }
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() { D…
- What is printed? java class Util { static int say(String s) { System.out.print(s); return 1; } } interface I { int X = Util.say("I"); }…