C++ friend Function and friend Classes

03-11-17 Course- CPP

One of the important concept of OOP is data hiding, that is, a nonmember function cannot access an object's private or protected data. But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member function which is  friend function and friend class.

friend Function in C++

If a function is defined as a friend function then, the private and protected data of class can be accessed from that function. The complier knows a given function is a friend function by its keyword friend. The declaration of friend function should be made inside the body of class (can be anywhere inside class either in private or public section) starting with keyword friend.


class class_name
{
    ......  ....   ........
    friend return_type function_name(argument/s);
    ......  ....   ........
}

Now, you can define friend function of that name and that function can access the private and protected data of that function. No keywords in used in function definition of friend function.

Working of friend function in C++ Programming

Example to Demonstrate working of friend Function


/* C++ program to demonstrate the working of friend function.*/
#include <iostream>
using namespace std;
class Distance
{
    private:
        int meter;
    public:
        Distance(): meter(0){ }
        friend int func(Distance);  //friend function
};
int func(Distance d)            //function definition
{
    d.meter=5;         //accessing private data from non-member function
    return d.meter;
}
int main()
{
    Distance D;
    cout<<"Distace: "<<func(D);
    return 0;
}

Output


Distance: 5

Here, friend function func() is declared inside Distance class. So, the private data can be accessed from this function.

Though this example gives you what idea about the concept of friend function, this program doesn't give you idea about when friend function is helpful.

Suppose, you need to operate on objects of two different class then,friend function can be very helpful. You can operate on two objects of different class without using friend function but, you program will be long, complex and hard to understand.

Example to operate on Objects of two Different class using friend Function


#include <iostream>
using namespace std;
class B;     // forward declaration
class A {
    private:
      int data;
    public:
      A(): data(12){ }
      friend int func(A , B);   //friend function Declaration
};
class B {
    private:
       int data;
    public:
       B(): data(1){ }
       friend int func(A , B);  //friend function Declaration
};
int func(A d1,B d2)
/*Function func() is the friend function of both classes A and B. So, the private data of both class can be accessed from this function.*/
{
   return (d1.data+d2.data);
}
int main()
    {
        A a;
        B b;
        cout<<"Data: "<<func(a,b);
        return 0;
    }

In this program, classes A and B has declared func() as a friend function. Thus, this function can access private data of both class. In this program, two objects of two different class A and B are passed as an argument to friend function. Thus, this function can access private and protected data of both class. Here, func() function adds private data of two objects and returns it to main function.

To work this program properly, a forward declaration of a class should be made as in above example(forward declaration of class B is made). It is because class B is referenced from class A using code: friend int func(A , B);. So, class A should be declared before class B to work properly.

friend Class in C++ Programming

Similarly like, friend function. A class can be made a friend of another class using keyword friend. For example:


........  .....  ........
class A{
   friend class B;      // class B is a friend class
   ..... ..... .....
}
class B{
   ..... ..... ..... 
}

When a class is made a friend class, all the member functions of that class becomes friend function. In this program, all member functions of class B will be friend function of class A. Thus, any member function of class B can access the private and protected data of class A.

If B is declared friend class of A then, all member functions of class B can access private data and protected data of class A but, member functions of class A cannot private and protected data of class B. Remember, friendship relation in C++ is granted not taken.