friend Functions and friend Classes in C++

You can define a friend function or a friend class to enable it to access private members in another class.                                       

C++ allows you to overload the stream insertion operator (<<) and the stream extraction opera­tor (>>). These operators must be implemented as friend nonmember functions. This section introduces friend functions and friend classes to prepare you to overload these operators.

Private members of a class cannot be accessed from outside the class. Occasionally, it is convenient to allow some trusted functions and classes to access a class’s private members.

C++ enables you to use the friend keyword to define friend functions and friend classes so that these trusted functions and classes can access another class’s private members.

Listing 14.4 gives an example that defines a friend class.                                                                         friend class

Listing 14.4 Date.h

1 #ifndef DATE_H
2
#define DATE_H
3
class Date
4 {
5     
public:
6     Date(
int year, int month, int day)
7     {
8       
this->year = year;
9       
this->month = month;
10       
this->day = day;
11    }
12
13   
friend class AccessDate;
14
15   
private:
16   
int year;
17   
int month;
18   
int day;
19 };
20
21
#endif

The AccessDate class (line 4) is defined as a friend class. So, you can directly access private data fields year, month, and day from the AccessDate class in Listing 14.5.

Listing 14.5 TestFriendClass.cpp

1 #include <iostream>
2
#include “Date.h”
3 using namespace std;
4
5
class AccessDate
6 {
7   
public:
8   
static void p()
9    {
10      Date birthDate(
2010, 3, 4);
11      birthDate.year =
2000;

12      cout << birthDate.year << endl;
13   }
14 };
15
16
int main()
17 {
18    AccessDate::p();
19
20   
return 0;
21 }

The AccessDate class is defined in lines 5-14. A Date object is created in the class. Since AccessDate is a friend class of the Date class, the private data in a Date object can be accessed in the AccessDate class (lines 11-12). The main function invokes the static function AccessDate::p() in line 18.

Listing 14.6 gives an example of how to use a friend function. The program defines the Date class with a friend function p (line 13). Function p is not a member of the Date class but can access the private data in Date. In function p, a Date object is created in line 23, and the private field data year is modified in line 24 and retrieved in line 25.

Listing 14.6 TestFriendFunction.cpp

1 #include <iostream>
2
using namespace std;
3
4
class Date
5 {
6     
public:
7     Date(
int year, int month, int day)
8     {
9       
this->year = year;
10       
this->month = month;
11       
this->day = day;
12    }
13   
friend void p();
14
15   
private:
16   
int year;
17   
int month;
18   
int day;
19 };
20
21
void p()
22 {
23    Date date(
2010, 5, 9);
24    date.year =
2000;
25    cout << date.year << endl;
26 }
27
28
int main()
29 {
30    p();
31
32   
return 0;
33 }

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *