Which of the following lists the three access specifiers that can be applied to class members in C++?

Difficulty: Easy

Correct Answer: public, private, and protected

Explanation:


Introduction / Context:
Access specifiers are fundamental to object oriented programming in C++. They control which parts of a program can directly use data members and member functions of a class. Correct use of access specifiers helps enforce encapsulation and information hiding. This question asks you to identify the three standard access specifiers available for class members in C++ and to distinguish them from terms used in other languages or contexts.


Given Data / Assumptions:

  • We are working specifically with C++ classes and structs.
  • We want the names of the language level access specifiers that govern member accessibility.
  • Other languages like Java or C sharp may use different access modifiers, which can be confusing.
  • The options mix correct C++ terminology with terms borrowed from other ecosystems.


Concept / Approach:
C++ defines three access levels for class members: public, private, and protected. public members are accessible from any code that has visibility of the class object. private members are accessible only within the class itself and its friends. protected members are accessible within the class, its friends, and derived classes, but not from unrelated code. Structs default to public access, and classes default to private access, but the specifiers themselves are the same. Other terms such as package, internal, sealed, interface, or final are not access specifiers in C++ itself, even if they appear in other languages or as unrelated C++ keywords.


Step-by-Step Solution:
Step 1: Recall that the standard C++ access specifiers are public, private, and protected. Step 2: Examine option a, which lists public, private, and protected together. Step 3: Compare with option b, which includes package, a Java term that is not a C++ access specifier. Step 4: Note that option c uses open, sealed, and internal, which may appear in other languages but not as C++ access keywords. Step 5: Recognise that option d lists interface, abstract, and final, which are either different kinds of modifiers or not access specifiers at all in C++.


Verification / Alternative check:
Any C++ reference or tutorial on classes shows examples such as class Example { public: void f; private: int x; protected: int y; }. These code samples consistently use public, private, and protected to mark sections and individual members. The language grammar recognises exactly these three as access specifiers, and they appear explicitly in the C++ standard. There is no C++ access specifier called package or internal in the core language.


Why Other Options Are Wrong:
Option b mixes correct terms public and private with package, which describes a packaging concept in Java and does not exist as a keyword in C++. Option c lists open, sealed, and internal, which are more typical of languages such as Kotlin or C sharp. Option d lists interface, abstract, and final, which are about type declarations or inheritance semantics in some languages, not about member access levels in C++.


Common Pitfalls:
A common pitfall for developers who use multiple languages is to mix language specific terminology and assume that features from one language exist in another with the same names. Another mistake is to forget that the default access differs between class and struct when no specifier is written. For exam questions, focus on the canonical trio public, private, and protected, and remember their basic visibility rules for members and inheritance.


Final Answer:
The three access specifiers for class members in C++ are public, private, and protected.

Discussion & Comments

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