Explanation of Friend Function

Please explain this statement through an example, All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship, with specification ki which function is granting and who is granted the friendship.

hello @Sakshi2004

class XYZ {
private:
   char ch='A';
   int num = 11;
public:
   /* This statement would make class ABC
    * a friend class of XYZ, this means that
    * ABC can access the private and protected
    * members of XYZ class. 
    */
   friend class ABC;
};
class ABC {
public:
   void disp(XYZ obj){
      cout<<obj.ch<<endl;
      cout<<obj.num<<endl;
   }
};

we have two classes XYZ(granting class) and ABC(granted class) . The XYZ class has two private data members ch and num , this class declares ABC as friend class. This means that ABC can access the private members of XYZ

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.