ProgrammingVery hardProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
class B { protected: int x = 1; };
class D : public B {
public:
    int f(D& d) { return d.x; }   // L1
    int g(B& b) { return b.x; }   // L2
};
  1. A. L2 only
  2. B. L1 only
  3. C. Both L1 and L2
  4. D. Neither

Correct answer: A. L2 only

Inside D, a protected member of B may be accessed only through an object of type D (or derived from D). b might be some other subclass of B, so b.x is not allowed.

More OOP with C++ and Java questions

Exams that ask this topic