Difficulty: Easy
Correct Answer: The member is visible inside its own class and to derived classes, but not to unrelated external code.
Explanation:
Introduction / Context:
Access specifiers control how class members can be used from different parts of a program. In C++ and many other object oriented languages, the three main access levels are public, protected and private. This question focuses on the meaning of protected, which is often used when designing class hierarchies.
Given Data / Assumptions:
Concept / Approach:
A protected member is accessible from within its own class and from classes derived from that class, but not from other unrelated code. This forms a middle ground between public, which is accessible everywhere, and private, which restricts access to the declaring class only. Protected access is useful when you want derived classes to be able to manipulate certain internal details of the base class while still hiding these details from external clients.
Step-by-Step Solution:
Step 1: Recall that public members are accessible from any code that can see the class.Step 2: Recall that private members are only accessible from within the class itself and its friends.Step 3: Protected members are in between, accessible to the class and its derived classes.Step 4: Option C states that the member is visible inside its own class and to derived classes, but not to unrelated code.Step 5: Options A, B, D and E either misidentify the scope or confuse protected with other concepts, so option C is correct.
Verification / Alternative check:
Consider class Base { protected: int value; }; and class Derived : public Base { void set() { value = 10; } };. The member value is accessible inside Derived because it is protected in Base. However, in a separate function void f() { Base b; b.value = 5; } the compiler will report an error, because value is not accessible from unrelated code. This matches the description given in option C.
Why Other Options Are Wrong:
Option A describes block scope rather than class member access. Option B corresponds more closely to public access, where members are visible everywhere. Option D claims the member is invisible even inside its own class, which would make it useless. Option E introduces namespaces, which are orthogonal to class member access.
Common Pitfalls:
Programmers sometimes use protected when they really want private, exposing more of the implementation than necessary to derived classes. Another common confusion is between protected inheritance and protected access, which are related but distinct concepts. For exam purposes, remember that protected members are visible inside the class and in derived classes, but not in arbitrary external code.
Final Answer:
The member is visible inside its own class and to derived classes, but not to unrelated external code.
Discussion & Comments