Difficulty: Easy
Correct Answer: A class may be declared as a friend.
Explanation:
Introduction / Context:
Friendship in C++ is an access-control feature that allows a specified function or class to access the private and protected members of another class. It is commonly used to implement symmetric operators, tight collaborations between classes, or non-member helper functions that require privileged access without making data public.
Given Data / Assumptions:
Concept / Approach:
Inside a class definition, you can declare either a specific function or another class as a friend. Doing so gives the friend full access to private/protected members of the declaring class. You cannot declare a particular data member as a friend; access always targets functions or whole classes. Friendship is to types or functions, not specific object instances, so “an object as a friend” is meaningless. Finally, the token friend is a reserved keyword and cannot be used as a class name.
Step-by-Step Solution:
Verification / Alternative check:
Create two classes and declare one as friend of the other. Access private fields in the friend class's methods; it compiles. Attempt to mark a single data member as friend or to friend an object variable; the compiler rejects these constructs.
Why Other Options Are Wrong:
Private data member as friend — syntactically and semantically unsupported.
Object as friend — friendship is granted to functions or classes, not instances.
friend as class name — violates the reserved keyword rule.
Common Pitfalls:
Final Answer:
A class may be declared as a friend.
Discussion & Comments