In C++ access control, which members are intended to be accessible only within the class and its derived classes (i.e., within the class hierarchy chain)? Choose the best match for “available only in the hierarchy”.

Difficulty: Easy

Correct Answer: Protected data members

Explanation:


Introduction:
Access specifiers in C++ control visibility of class members. This question targets the meaning of protected and how it differs from public and private with respect to inheritance.


Given Data / Assumptions:

  • Three main access levels: public, protected, private.
  • Focus: visibility across class boundaries and along inheritance chains.
  • Typical, non-friend contexts assumed.


Concept / Approach:
Protected members are accessible within the class that declares them and within derived classes (subject to access rules and friendship). They are not accessible to unrelated external code. That matches the idea “available only in the class hierarchy chain.”


Step-by-Step Solution:
1) Public: accessible everywhere — not restricted to the hierarchy.2) Private: accessible only within the declaring class — not even in derived classes.3) Protected: accessible in the declaring class and in derived classes — the hierarchy chain.4) Therefore, “Protected data members” is the correct choice.


Verification / Alternative check:
Attempting to access a protected member through an unrelated object from outside the hierarchy fails to compile, while a derived class method can access it.


Why Other Options Are Wrong:
Public data members: accessible everywhere, not limited to the hierarchy.Private data members: not accessible in derived classes (except via friends or accessors).Member functions: access depends on their specified level; “member functions” is too broad.


Common Pitfalls:
Assuming protected is similar to package-private from other languages. In C++, protected is strictly about class and derived-class access, not modules or namespaces.


Final Answer:
Protected data members

Discussion & Comments

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