Vidyalelo
C++ Programming · Q28

Classes and Objects in C++

Programming · C++ Programming · question 28

Q28

Which access specifier allows members of a class to be accessed only by member functions of the same class and its derived classes in C++?

A.
protected
Answer
B.
public
C.
private
D.
friend

Answer: Option A

Solution

Answer: Option A
Solution:
Access specifiers in C++ determine the visibility and accessibility of class members.

The protected access specifier allows members of a class to be accessed by:

1. Member functions of the same class

2. Member functions of its derived (child) classes

This makes protected different from private, which restricts access only to the class itself, and from public, which allows access from anywhere.

Now let’s see why the other options are incorrect:

Option B: public — Members are accessible from anywhere in the program, which is more permissive than required by the question.

Option C: private — Members are accessible only within the same class, not by derived classes, so this is too restrictive.

Option D: friendfriend is not an access specifier; it is a keyword used to allow a specific external function or class to access private or protected members of another class. It doesn’t define a general access level.

Conclusion: The protected access specifier is the one that allows class members to be accessed within the same class and its derived classes.

Hence, the correct answer is Option A: protected.