1. Introduction
We all know that one can not access private members of a class outside. There is an alternative for this concept and we call the alternative as a Friend Functions. A Friend Function can access the private member of the class, and a Friend Class can also access the private member of a class.
In this article, we will see the below concepts with examples:
- Creating a global friend function
- Friend function which is actually a member of a class
- Creating a friend class
2. Global Friend Function
A global function which does not belong to any class can have a friendship with one or more class or classes. This global function will not have any constraint to access the private data member of the class. Have a look at the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include "stdafx.h" #include <iostream> using namespace std; //===================================================================== //Example 01: A Global Function having friendship with Point3d class //===================================================================== //Sample 01: A class to denote 3 points class Point3d { private: int m_x; int m_y; int m_z; public: Point3d(int x, int y, int z) { m_x = x; m_y = y; m_z = z; } //Sample 02: Point3d claiming Increment_point function as // its friend. friend void Increment_Point(Point3d& pt); //Sample 03: To print the Members void Printdata() { cout<<"X: "<<m_x<<endl; cout<<"Y: "<<m_y<<endl; cout<<"Z: "<<m_z<<endl; } }; //Sample 04: Friend Function that makes Increment of the 3d point. void Increment_Point(Point3d& pt) { //Note that the private member of the class is changed //by the global function pt.m_x++; pt.m_y++; pt.m_z++; } int _tmain(int argc, _TCHAR* argv[]) { //Sample 05: Create the Object and print the Initial Co-Ordinates Point3d pt1(10,10,12); pt1.Printdata(); cout<<endl; //Sample 06: Increment the Points using the Friend Function and print // the points. Increment_Point(pt1); pt1.Printdata(); } |
In the above example, we define the class
Point3d
to explain the Friend Function usage. In this class we declare three private members. We will access these private members through the global function Increment_Point. Note, this function takes the
CPoint3d
as a reference parameter. Also, note that the
Increment_Point
function is changing the x, y, z coordinates of the passed-in pt object.
2.1 Declaration
Now look at the code snippet marked as //Sample 02. Here, we specify the function Increment_Point as a friend to the class. You can see this otherwise like, class
Point2d
claims that Increment_Point as its friend. Once this friendship claiming is done through the friend keyword, the function can access the private member(s) of the class in which we claim the friendship.
2.2 Client Usage
In the main function at the code snippet defined by //Sample 05, we create a
Poind3d
object
pt1
with the coordinate values of (10,10,12). Then, we print the values using the public member function
PrintData
.
In the code snippet marked by //Sample 06, the point
pt1
object is passed to the global function
Increment_Point
and then the data is printed. As this global function is a friend to the class
Point3d
, it raises each coordinate values by one even though they are in private scope. The output of our first example is below:

Global Friend Function Example 1
Note, the
Increment_Point
is a global function, and it is not a class member function. Inside the class we specified a friend keyword and then specified the global function signature. One can imagine that class is claiming a friendship with the function. Hence, it specifies the function name with its full signature. The below picture explains this.

Class Claiming Friendship with a function
3. Class Member Function as A Friend
Like a global function, a class member function can have a friendship with some other class. Now the class member function can have access to the private member of the class which grants friendship to it. In this example, we claim the member function
MovePointbyOne
of
CGraphicUtil
as a friend of the class
CPoint2d
. Have a look at the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include "stdafx.h" #include <iostream> using namespace std; //===================================================================== //Example 02: A Class having friendship with some other class member //===================================================================== //Sample 01: Forward Declaration as CGraphicUtil going to refer it. class CPoint2d; //Sample 02: CGraphicUtil class class CGraphicUtil { public: //Sample 03: This member will be claimed as friend by the CPoint2d. // This class knows CPoint2d because of forward declaration. // But it does not its internal members. Hence, Note that implementation // is delayed till we define cpoint2d fully. void MovePointbyOne(CPoint2d& pt1); void some_other_function() { cout<<"Does nothing"; } }; //Sample 04: CPoint2d class class CPoint2d { private: int m_x; int m_y; public: CPoint2d(int x, int y) { m_x = x; m_y = y; } void Print() { cout<<"X: "<<m_x<<endl; cout<<"Y: "<<m_y<<endl; } //Sample 05: CPoint2d claiming MovePointbyOne as Friend function. //Note the Order: CGraphicUtil layout is already defined before the // CPoint2d. friend void CGraphicUtil::MovePointbyOne(CPoint2d& pt1); }; //Sample 06: We delayed this implementation intentionally. // Now the CGraphicUtil is aware of the Layout of the CPoint2d void CGraphicUtil::MovePointbyOne(CPoint2d &pt1) { pt1.m_x++; pt1.m_y++; } int _tmain(int argc, _TCHAR* argv[]) { //Sample 07: Create a CPoint2d object and print the Co-Ordinates CPoint2d point(10,10); cout<<"Point Co-Ordinates"<<endl; point.Print(); //Sample 08: Increment the Co-Ordinates using CGraphicsUtil cout<<endl<<"Point Co-Ordinates After Increment"<<endl; CGraphicUtil util; util.MovePointbyOne(point); point.Print(); } |
Code Explanation
Sample 01
Here, we keep a Forward declaration for the class
CPoint2d
. This declaration tells to C++ that we will define the
CPoint2d
class later in the file.
Sample 02
Here, we defined a class called
CGraphicUtil
.
Sample 03
Here, we declared the function
MovePointbyOne
. Note, the implementation for this function is not provided yet. This is because the compiler is not aware of the layout of the
CPoint2d
structure at this moment. But, it knows that such a type exists because of Forward Declaration. Therefore, we provided only function prototype here. Providing the implementation in the class itself will end up in the error — ‘Error C2027: use of undefined type CPoint2d’.
Sample 04
Here, we Define our
CPoint2d
class.
Sample 05
CPoint2d
class claims, the member function
MovePointbyOne
of the class
CGraphicUtil
, is in friendship with it. Here, one more point we need to be aware is that the class
CGraphicUtil
is already defined and hence we will not get any problem.
Sample 06
In Code snippet 03, we provided only the declaration for the
MovePointbyOne
, as
CPoint2d
is not defined at that time. Snippet 05 of the
CPoint2d
claimed the friendship. So now, we have both
CGraphicUtil
and
CPoint2d
is defined and the compiler is aware of the structure and memory requirement of both the classes. With all that said, now it is time to implement the
CGraphicUtil
member function
MovePointbyOne
as given in the //sample 06 code snippet. Note, this function accepts the
CPoint2d
instance as a parameter and increments the private members by one.
Sample 07
We create a
CPoint2d
object called
point
with the coordinate value of 10,10. Then these coordinate values are printed using the public member function
Print()
.
Sample 08
Here, we Create the
CGraphicUtil
object called
util
. Then the function
MovePointbyOne
is called on the
util
object by passing the point object created in the snippet 07. We then print the increment’d coordinate values using the public member function
.
In the below picture, we can see that
CPoint2d
claiming a friendship. This time the friend is a member function in some other class. Class
CPoint2d
uses friend keyword and while specifying the name of the function it includes the class name and scope resolution operator ‘
::
’.

Class Member Function As Friend
The output of executing the the example is below:

Class Member as a Friend: Example 2
4. Friend Class
A class can grant friendship to some other class. Say, for example, class A grants friendly access to class B. Now, all the member functions of class B can have access to private data members of class A. Also, we call class B as Friend Class. Let us see this with an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include "stdafx.h" #include <iostream> using namespace std; //==================================================================================== //Example 03: Frield Class //==================================================================================== //Sample 01: A 2d point class defined. It claims friendship with the class CPoint2d { private: int m_x; int m_y; public: CPoint2d(int x, int y) { m_x = x; m_y = y; } void print() { cout<<"X: "<< m_x << " Y: " << m_y << endl; } //Sample 02: Now, All the member functions of CPointUtil can access // the private data of this class. friend class CPointUtil; }; //Sample 03: CPointUtil changing the private data member of the supplied // CPoint2d class. class CPointUtil { public: void increment_x(CPoint2d& pt) { pt.m_x++; } void increment_y(CPoint2d& pt) { pt.m_y++; } void increment_both(CPoint2d& pt) { pt.m_x++; pt.m_y++; } }; int _tmain(int argc, _TCHAR* argv[]) { //Sample 04: Create a CPoint2d instance and print the co-ordinates CPoint2d pt1(5,4); pt1.print(); //Sample 05: Use increment_x method to increment x coordinate CPointUtil putil; cout<<"Increment only x and print"<<endl; putil.increment_x(pt1); pt1.print(); //Sample 06: Use increment_both to increment both x and y Co-Ordinates cout<<"Increment both x,y and print"<<endl; putil.increment_both(pt1); pt1.print(); } |
Code Explanation
Sample 01
We define
CPoint2d
class. This class has two private data members which represent X & Y coordinate of the class.
Sample 02
Class
CPoint2d
specifies that
CPointUtil
is friend. Once this friendship is held, all the members of the
CPointUtil
can have entry to the private data members of the class
CPoint2d
.
Sample 03
CPointUtil
class has only three member functions. Each function gets a
CPoint2d
class instance as a parameter. As already told, the class
CPoint2d
gave the friendly access to the full class
CPointUtil
. As a result, all the member functions of the
CPointUtil
can access the private data members.
Sample 04
We create a two dimensional point object named
pt1
with the coordinate values of (5,4). Then, we print the coordinates in the console window.
Sample 05
We create the
putil
object from the class
CPointUtil
. Then we call the member function increment_x by passing
CPoint2d
instance
pt1
as a parameter. After the call, the co-ordinate values are printed.
Sample 06
Finally, we make a call to the function increment_both to raise x and y co-ordinate values by one.

Friend Class Example
Categories: C++
Tags: Friend Class, Friend Function