ProgrammingVery hardProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
struct A { int x = 1; };
struct B : protected A { };
struct C : B { int get() { return x; } };
int main() {
    C c;
    cout << c.get();   // L1
    cout << c.x;       // L2
}
  1. A. L2 only
  2. B. L1 only
  3. C. Both
  4. D. Neither

Correct answer: A. L2 only

Protected inheritance makes A's public members protected in B; C inherits them as protected, so C's own member function can use x, but code outside the hierarchy (main) cannot.

More OOP with C++ and Java questions

Exams that ask this topic