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:
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:
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:
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.
Discussion & Comments