Sei sulla pagina 1di 4

#include <iostream> using namespace std; class CSquare; class CRectangle { int width, height; public: int area

() {return (width * height);} void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) {side=a;} friend class CRectangle; }; void CRectangle::convert (CSquare a) { width = a side; height = a side; } int !ain () { CSquare sqr; CRectangle rect; sqr set_side("); rect convert(sqr); cout ## rect area(); return $; } Just as we have the possibility to define a friend function, we can also define a class as friend of another one, granting that first class access to the protected and private members of the second one. In this example, we have declared CRectangle as a friend of CSquare so that CRectangle member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of the square. You may also see something new at the beginning of the program: an empty declaration of class CSquare. This is necessary because within the declaration of CRectangle we refer to !quare "as a parameter in convert()#. The definition of CSquare is included later, so if we did not include a previous empty declaration for CSquare this class would not be visible from within the definition of CRectangle. onsider that friendships are not corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle does not consider CSquare to be a friend, so CRectangle can access the protected and private members of CSquare but not the reverse way. $f course, we could have declared also CSquare as friend of CRectangle if we wanted to. %nother property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.

%include #iostrea!& class ' { (( ' declares ) as a *riend *riend class ); +rivate: void +rivate,rint() { std::cout ## -hello, world- ## std::endl; } }; class ) { +u.lic: )() {

' .; (( and ) now has access to '/s +rivate !e!.ers . +rivate,rint();

};

int !ain() { ) a; return $; } It provides additional functionality which is &ept outside the class. It provides functions with data which is not normally used by the class. It provides a means of reducing algorithmic complexity by giving direct access to private and protected members to an external specified scope while not ma&ing the members more broadly accessible than necessary.

%include #iostrea!& using na!es+ace std; class 0ourClass { *riend class 0our1therClass; (( 2eclare a *riend class +u.lic: 0ourClass() : to+Secret($){} void +rint3e!.er() { cout ## to+Secret ## endl; } +rivate: int to+Secret; }; class 0our1therClass { +u.lic: void change( 0ourClass4 5c, int 6 ){5c to+Secret = 6;} }; int !ain() { 0ourClass 5c7; 0our1therClass 5oc7; 5c7 +rint3e!.er(); 5oc7 change( 5c7, 8 ); 5c7 +rint3e!.er(); } 'riendship is not mutual unless explicitly specified as such. In the above example, member functions of Your lass cannot access the private members of Your$ther lass. % managed type cannot have any friend functions, friend classes, or friend interfaces. 'riendship is not inherited, meaning that classes derived from Your$ther lass cannot access Your lass(s private members. 'riendship is not transitive, so classes that are friends of Your$ther lass cannot access Your lass(s private members. The following figure shows four class declarations: )ase, *erived, a'riend, and another'riend. $nly class a'riend has direct access to the private members of )ase "and to any members )ase might have inherited#.

%include #iostrea!& using na!es+ace std; class 35Class { (( 2eclare a *riend class *riend class SecondClass; +u.lic: 35Class() : Secret($){} void +rint3e!.er() { cout ## Secret ## endl; } +rivate: int Secret; }; class SecondClass { +u.lic: void change( 35Class4 5ourclass, int 6 ) { 5ourclass Secret = 6; } }; void !ain() { 35Class !5_class; SecondClass sec_class; !5_class +rint3e!.er(); sec_class change( !5_class, 8 ); !5_class +rint3e!.er(); } % class can also be declared to be the friend of some other class. +hen we create a friend class then all the member functions of the friend class also become the friend of the other class. This requires the condition that the friend becoming class must be first declared or defined "forward declaration#.

we declared friend class !econd lass, in the class -y lass, so we can access !ecret in the class !econd lass. %nother property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.

Potrebbero piacerti anche