In C++ access control, which keyword is used specifically to control access to a class member (i.e., to set its accessibility level within and outside the class)?

Difficulty: Easy

Correct Answer: Protected

Explanation:


Introduction / Context:
C++ provides three access specifiers to control the visibility of class members: public, protected, and private. Choosing the correct specifier is fundamental to encapsulation, information hiding, and safe inheritance hierarchies. This question asks you to identify the keyword among the options that actually serves as an access-control specifier.


Given Data / Assumptions:

  • We are discussing class member accessibility in C++.
  • Valid access specifiers are public, protected, and private.
  • Other listed tokens may exist in C/C++ but are not access specifiers.


Concept / Approach:

Access specifiers determine who can see or use class members. Inheritance also interprets them: under public inheritance, public stays public, protected stays protected, and private remains inaccessible to derived classes. Among the choices, only protected is a real access specifier. The others either have different purposes or are not standard C++ keywords for access control.


Step-by-Step Solution:

List true access specifiers: public, protected, private. Compare each option with this set. Identify that only “Protected” matches an access specifier. Conclude that “Protected” is the correct answer.


Verification / Alternative check:

Write a sample class and mark a member as protected. Attempt access from outside the class (fails) and from a derived class (succeeds), demonstrating the role of the protected specifier.


Why Other Options Are Wrong:

Default: no such access specifier keyword; “default access” is contextual (class vs. struct).

Break: used to exit loops/switch, not for access control.

Asm: non-standard/extension keyword for inline assembly, unrelated to access.


Common Pitfalls:

  • Confusing “default access” (private in class, public in struct) with an actual keyword.
  • Assuming protected means publicly accessible—protected limits access to the class and its descendants.


Final Answer:

Protected

More Questions from Objects and Classes

Discussion & Comments

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