In C++, which of the following is true for private members of a class ?
2025
In C++, which of the following is true for private members of a class ?
- A.
They can be accessed directly by any other class
- B.
They are accessible only within the same class
- C.
They are accessible in the derived classes
- D.
They are accessible through this pointer
Attempted by 54 students.
Show answer & explanation
Correct answer: B
In C++, access specifiers control the visibility of class members. Private members are designed to be hidden from external code, ensuring encapsulation and data integrity. They can only be accessed directly by member functions of the same class or by friend functions/classes explicitly granted access. This restriction prevents unintended modification from outside, which is why Option B is correct.
Option A is incorrect because private members cannot be accessed by any other class without friendship.
Option C is misleading; while derived classes can access protected or public members, they cannot directly access private members of the base class.
Option D is incorrect because the 'this' pointer only refers to the current object and does not bypass access restrictions. Therefore, private members remain confined within their defining class scope.