Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Friend Class And Friend Function in C++

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:

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

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

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:

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

Print

.

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

Class Member Function As Friend

The output of executing the the example is below:

Class Member as a Friend: Example 2

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:

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 3

Friend Class Example

Categories: C++

Tags: ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.