In C++ access control, private members of a base class cannot be accessed directly by member functions of a derived class, nor by objects of the derived class. Judge the statement.

Difficulty: Easy

Correct Answer: Correct — base private members are inaccessible to derived code.

Explanation:


Introduction / Context:
This item tests knowledge of C++ access specifiers and inheritance rules. Specifically, it focuses on whether a derived class (and objects of that class) can directly access private members of its base class.


Given Data / Assumptions:

  • A base class has private data/functions.
  • A derived class inherits publicly/privately/protectedly from the base.
  • We consider direct access from derived code or through derived-class objects.


Concept / Approach:
In C++, private members are only accessible within the declaring class and its friends. Inheritance does not change the accessibility of private members; it only affects how base class members are exposed through the derived interface. A derived class cannot directly access a base’s private members; it must use base public/protected interfaces. Objects (instances) of the derived class also cannot access those private members because access control is enforced at compile time based on the member’s declaring class.


Step-by-Step Solution:

Base declares some private members. Derived code attempts to read/write them directly → compile error. Use base public/protected member functions to manipulate those members instead.


Verification / Alternative check:
If you truly need direct access, you can declare the derived class or specific functions as friend of the base. Alternatively, change the members to protected if design permits. Otherwise, provide proper getters/setters or protected hooks in the base.


Why Other Options Are Wrong:

  • Direct access from derived or objects is disallowed — contradicts C++ rules.
  • Virtual has no effect on access control; it only affects dynamic dispatch.
  • Public inheritance does not expose base private members.


Common Pitfalls:
Assuming inheritance implies visibility; it does not. Access control is orthogonal to inheritance kind. Also, confusing protected (accessible in derived) with private (not accessible).


Final Answer:
Correct — base private members are inaccessible to derived code.

More Questions from Inheritance

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion