//Friend function Friend class#include <iostream>using namespacestd;classpointb{ Public: FriendclassPOINTC; //class Pointc is a friend of class pointb--meaning that the class Pointc object can invoke all members of POINTB voidTest () {; }Private: intx; inty;};classpointc{ Public: voidPrintfpointb () {//call the private property of its friend classcout << pb.y <<Endl; //call the member function of POINTBPB. Test (); }Private: POINTB pb;};classpointa{ Public: //friend functionFriendvoidFriendfun (pointa*pin); intGeta () {returnA; } intGetb () {returnb; }Private: intA; intb;};//Friend Connections//Const (C-language) counterfeit//register CPU side of the Small eunuch (register)//typedef mixed King (alias)//friend function//A friend must have a parameter that is the object pointer of the class in which the friend function is located (otherwise it cannot access the private properties of the object)//A friend function is a global function//encapsulation of class with friend function failurevoidFriendfun (pointa*pin) { //private members of a class can be accessed directlyPin->a =Ten; Pin->b = -;}voidProtecta () {Pointa*P1 =NewPointa (); Friendfun (p1); cout<<"a="<< P1->geta () <<"; b="<< P1->getb () <<Endl; if(p1!=NULL) { DeleteP1; }}voidMain () {protecta (); System ("Pause");}
C + + friend class, friend function