Home > OOP > Friend Class and Friend Function in C++

C++ Friend Function

One of the most important features of Object-oriented programming is data hiding. It means that you cannot access private and protected members of a class outside the class. But to access these data members there are some mechanisms provided by C++. The c++ friend function is also one of these. When you define a function as a friend function to a class, then it can access private and protected members of the class outside of class. You can define a friend function in c++ by using Friend keyword with the function declaration.

Declaring a Friend Function in c++

Syntax to declare a friend function in c++ to a class is:

class class_name
{
     friend return_type function_name(argument/s);
};
return_type functionName(argument/s)
{
     // Private and protected data of className can be accessed from
     // this function because it is a friend function of className.
}

friend is the reserved word to declare that this function is a c++ friend function.

return_type is any of the data types available in c++.

function_name is the name of the function.

Below is a simple program example showing how to create a friend function in c++.

Example program: –

#include "stdafx.h"
#include "iostream"

using namespace std;
class B;
class A
{
     int a;
     
     public:
          A()
          {
               a = 10;
          }
          friend void sum(A, B);
};
class B
{
     int b;

     public:
          B()
          {
               b = 20;
          }
          friend void sum(A, B);
};

void sum(A objA, B objB)
{
     int temp;
     temp = objA.a + objB.b;
     cout << "Sum of two class data members in freind function = " << temp << endl;
}
int main()
{
     A objA;
     B objB;
     sum(objA, objB);
     return 0;
}

Output: –

Friend Function in c++ example

Friend Class in c++

Just like c++ friend functions, C++ allows the user to create a friend class for a class. You can do this by using friend keyword with the class name.

Syntax to write a friend class is:


class B;
class A
{
     // declaring class B as friend of class A.
     Friend class B;
};

Here most important thing to note is that when a class is made a friend of another class all of its member functions will become friend functions to other class. i.e. all member functions of class B are friend functions to class A. Thus allowing functions of class B to access private and protected data members of class A. But member function of class A cannot access private and protected data members of class B. (i.e. not symmetric).

Example program: –


#include "stdafx.h"
#include "iostream"

using namespace std;
class A
{
     int a;
     public:
          A()
          {
               a = 10;
          }
          friend class B;
};
class B
{
     public:
          void show(A objA)
          {
               cout << "Value of a in friend class B = " << objA.a<<endl;
          }
};

int main()
{
     A objA;
     B objB;
     objB.show(objA);
     return 0;
}

Output: –
Friend class
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.

Leave a Reply

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

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