ProgrammingHardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
class B { public: void f() { cout << "f"; } void g() { cout << "g"; } };
class D : private B { public: using B::f; };
int main() {
D d;
d.f(); // L1
d.g(); // L2
}- A. L1 only
- B. Both
- C. L2 only
- D. Neither
Correct answer: C. L2 only
Private inheritance makes all of B's members private in D. A using-declaration in D's public section re-exposes f only; g stays inaccessible from main.
More OOP with C++ and Java questions
- What is printed? cpp struct A { void hi() { cout << "A"; } }; struct B { void hi(int) { cout << "B"; } }; struct C : A, B { }; int main()…
- 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…