A friend function can be used to

2014

A friend function can be used to

  1. A.

    avoid arguments between classes.

  2. B.

    allow access to classes whose source code is unavailable.

  3. C.

    allow one class to access an unrelated class.

  4. D.

    None of the above

Attempted by 144 students.

Show answer & explanation

Correct answer: C

Answer: allow one class to access an unrelated class.

Explanation: A friend function or a friend class is explicitly granted by a class to access that class's private and protected members. This allows a non-member function or an unrelated class to work with the internals of the declaring class when necessary.

  • How friendship is granted: the class that wants to allow access declares the function or class as a friend.

  • Friendship applies only as declared: it is not inherited or automatically reciprocal.

  • Common uses: operator overloading, helper functions, or tightly coupled classes that need direct access to internal representation.

  • Encapsulation note: use friendship sparingly because it breaks strict encapsulation; the declaring class controls who is granted access.

Key takeaway: friend relationships allow explicit access to another class's internals and are declared by the class whose members are being accessed.

Explore the full course: Mppsc Assistant Professor