OBJECT ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING
Friends Classes and Functions
Friend functionsFriend functions
 A friend function of a class is defined outside that class's
scope, yet has the right to access the non-public (and
public) members of the class
 Let’s say you want a function to operate on objects of
two different classes and access their private data, you
would need to use a friend function
CPS235:Inheritance 2
Example ofExample of friendfriend functionsfunctions
class beta; //req for frifunc declaration
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend int frifunc(alpha, beta); //friend
func
declaration
};
class beta
{
private:
int data;
public:
beta():data(7) {}
friend int frifunc(alpha,beta);
//friend func
declaration
};CPS235:Inheritance 3
A class cannot be referred to
until it has been declared
Example ofExample of friendfriend functionsfunctions
int frifunc(alpha a, beta b) //friend func
defined
{
return (a.data + b.data);
}
void main()
{
alpha aa;
beta bb;
cout<<frifunc(aa,bb)<<endl; //friend func
called
getch();
}
CPS235:Inheritance 4
friendfriend classesclasses
 The member functions of a class can all be made friends
at the same time when you make the entire class a
friend
 If class alpha declares class beta as a friend class, then all
member functions of class beta can access private data
of class alpha
 It is also possible to declare only one member function
of another class to be a friend
CPS235:Inheritance 5
friendfriend classesclasses
class beta;
class alpha
{
private:
int data;
public:
alpha():data(3) {}
friend class beta; //friend class
declaration
};
class beta
{
public:
void func(alpha a)
{
cout<<"alpha's data:
"<<a.data;
}
};
CPS235:Inheritance 6
friendfriend classesclasses
void main()
{
alpha aa;
beta bb;
bb.func(aa);
getch();
}
CPS235:Inheritance 7
Class member functions asClass member functions as
friendsfriends
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 void CRectangle::convert(CSquare);
};
CPS235:Inheritance 8
Class member functions asClass member functions as
friendsfriends
void CRectangle::convert (CSquare a) {
width = a.side;
height = a.side;
}
int main () {
CSquare sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
getch();
return 0;
}
CPS235:Inheritance 9
Summary (friends)Summary (friends)
 Even though the prototypes for friend functions appear
in the class definition, friends are not member functions
 Friend declarations can be placed anywhere in a class
definition either in public, private or protected sections
 Violates the data hiding principle of classes, so it should
be avoided as much as possible
 Friendship is granted, not taken i.e., for class B to be a
friend of class A, class A must explicitly declare that
class B is its friend
 The friendship relation is neither symmetric nor
transitive
CPS235:Inheritance 10

Friends function and_classes

  • 1.
    OBJECT ORIENTED PROGRAMMINGOBJECTORIENTED PROGRAMMING Friends Classes and Functions
  • 2.
    Friend functionsFriend functions A friend function of a class is defined outside that class's scope, yet has the right to access the non-public (and public) members of the class  Let’s say you want a function to operate on objects of two different classes and access their private data, you would need to use a friend function CPS235:Inheritance 2
  • 3.
    Example ofExample offriendfriend functionsfunctions class beta; //req for frifunc declaration class alpha { private: int data; public: alpha():data(3) {} friend int frifunc(alpha, beta); //friend func declaration }; class beta { private: int data; public: beta():data(7) {} friend int frifunc(alpha,beta); //friend func declaration };CPS235:Inheritance 3 A class cannot be referred to until it has been declared
  • 4.
    Example ofExample offriendfriend functionsfunctions int frifunc(alpha a, beta b) //friend func defined { return (a.data + b.data); } void main() { alpha aa; beta bb; cout<<frifunc(aa,bb)<<endl; //friend func called getch(); } CPS235:Inheritance 4
  • 5.
    friendfriend classesclasses  Themember functions of a class can all be made friends at the same time when you make the entire class a friend  If class alpha declares class beta as a friend class, then all member functions of class beta can access private data of class alpha  It is also possible to declare only one member function of another class to be a friend CPS235:Inheritance 5
  • 6.
    friendfriend classesclasses class beta; classalpha { private: int data; public: alpha():data(3) {} friend class beta; //friend class declaration }; class beta { public: void func(alpha a) { cout<<"alpha's data: "<<a.data; } }; CPS235:Inheritance 6
  • 7.
    friendfriend classesclasses void main() { alphaaa; beta bb; bb.func(aa); getch(); } CPS235:Inheritance 7
  • 8.
    Class member functionsasClass member functions as friendsfriends 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 void CRectangle::convert(CSquare); }; CPS235:Inheritance 8
  • 9.
    Class member functionsasClass member functions as friendsfriends void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4); rect.convert(sqr); cout << rect.area(); getch(); return 0; } CPS235:Inheritance 9
  • 10.
    Summary (friends)Summary (friends) Even though the prototypes for friend functions appear in the class definition, friends are not member functions  Friend declarations can be placed anywhere in a class definition either in public, private or protected sections  Violates the data hiding principle of classes, so it should be avoided as much as possible  Friendship is granted, not taken i.e., for class B to be a friend of class A, class A must explicitly declare that class B is its friend  The friendship relation is neither symmetric nor transitive CPS235:Inheritance 10