An ordinary C++ member function has access privileges for all data members…
2017
An ordinary C++ member function has access privileges for all data members declared in __________, irrespective of whether they are public, protected, or private.
Answer: A. the class of which it is member — Concept: C++ access control is class-based. A member function of a class has access privileges for the names that the class can access, including public,…
- A.
the class of which it is member
- B.
the object of which it is a member
- C.
the public part of its class
- D.
the private part of its class
Attempted by 509 students.
Show answer & explanation
Correct answer: A
Concept: C++ access control is class-based. A member function of a class has access privileges for the names that the class can access, including public, protected, and private data members. Objects provide storage for non-static data, but they do not define the function’s access privileges.
Application: The clarified stem asks for the declaration scope that contains all data members, irrespective of access specifier. That scope is the class whose member function is being defined, so the completion is “the class of which it is member.”
Cross-check: Consider a member function using both private and public data:
class Box {
int value;
public:
bool same(const Box& other) const {
return value == other.value;
}
};The body of Box::same may name the private value member and could equally name a public member. This permission follows from Box class membership; the access specifier does not restrict Box’s own member-function body.
Contrast:
the object of which it is a member — an object stores instance data and supplies this during an ordinary call, but access privileges are determined by class membership.
the public part of its class — this is only one access category and does not cover protected or private members.
the private part of its class — this is also only one access category and does not cover public or protected members.
Result: the class of which it is member.