Session 15
Friend functions and Friend classes
Contents
Objectives
15.1 Friend functions
15.1.1 A demonstration on the use of friend functions
15.2 Friend classes
15.1.1 A demonstration on the use of friend classes
Summary
Objectives
After reading this lesson you should be able to:
Understand the concept of friends
Create friend functions
Create friend classes
15.1 Friend functions
In principle, private and protected members of a class cannot be accessed from outside
the same class in which they are declared. However, this rule does not affect friends.
Friends are functions or classes declared as such.
If we want to declare an external function as friend of a class, thus allowing this function
to have access to the private and protected members of this class, we do it by declaring a
prototype of this external function within the class, and preceding it with the keyword
friend:
1
15.1.1 A demonstration on the use of friend functions
1. #include<iostream.h>
2. #include<conio.h>
3. class CRectangle {
4. int width, height;
5. public:
6. void set_values (int, int);
7. int area () {return (width * height);}
8. friend CRectangle duplicate (CRectangle);
9. };
10.void CRectangle::set_values (int a, int b) {
11. width = a;
12. height = b;
13. }
14. CRectangle duplicate (CRectangle rectparam)
15. {
16. CRectangle rectres;
17. rectres.width = rectparam.width*2;
18. rectres.height = rectparam.height*2;
19.return (rectres);
20. }
21.void main () {
22. clrscr();
23. CRectangle rect, rectb;
24. rect.set_values (2,3);
25. rectb = duplicate (rect);
26. cout << rectb.area();
27. getch();
28. }
Output:
24
The duplicate function is a friend of CRectangle. From within that function we have been
able to access the members width and height of different objects of type CRectangle,
which are private members. Notice that neither in the declaration of duplicate() nor in its
later use in main() have we considered duplicate a member of class CRectangle. It isn't! It
simply has access to its private and protected members without being a member.
2
The friend functions can serve, for example, to conduct operations between two different
classes. Generally, the use of friend functions is out of an object-oriented programming
methodology, so whenever possible it is better to use members of the same class to
perform operations with them. Such as in the previous example, it would have been
shorter to integrate duplicate() within the class CRectangle.
15.2 Friend classes
Just as we have the possibility to define a friend function, we can also define a class as
friend of another one, granting that second class access to the protected and private
members of the first one.
15.1.1 A demonstration on the use of friend functions
1. #include<iostream.h>
2. #include<conio.h>
3. class CSquare;
4. class CRectangle {
5. int width, height;
6. public:
7. int area ()
8. {return (width * height);}
9. void convert (CSquare a);
10. };
11.class CSquare {
12.private:
13.int side;
14.public:
15.void set_side (int
a)
16. {side=a;}
17.friend class CRectangle;
18. };
19.void CRectangle::convert (CSquare a) {
20. width = a.side;
21. height = a.side;
22. }
3
23.void main () {
24. clrscr();
25. CSquare sqr;
26. CRectangle rect;
27. sqr.set_side(4);
28. rect.convert(sqr);
29. cout << rect.area();
30. getch();
31. }
Output:
16
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 CSquare (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.
Consider 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. Of course, we could have declared also
CSquare as friend of CRectangle if we wanted to.
Another property of friendships is that they are not transitive: The friend of a friend is not
considered to be a friend unless explicitly specified.
Summary
Friends are functions or classes declared in order to access the private and
protected members of a class.
Friend classes are declared in order to grant the second class access to the
protected and private members of the first one