Friend Functions

Friend Functions

One of the important concepts of OOP is data hiding, i.e., 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 functions.
This is done using a friend function or/and a friend class.

friend Function in C++

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

Declaration of friend function in C++

class class_name
{
    ... .. ...
    friend return_type function_name(argument/s);
    ... .. ...
}
Now, you can define the friend function as a normal function to access the data of the class. No friend keyword is used in the definition.
class className
{
    ... .. ...
    friend return_type functionName(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.
    ... .. ...
}

Example 1: 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 function
        friend int addFive(Distance);
};

// friend function definition
int addFive(Distance d)
{
    //accessing private data from non-member function
    d.meter += 5;
    return d.meter;
}

int main()
{
    Distance D;
    cout<<"Distance: "<< addFive(D);
    return 0;
}
Output
Distance: 5
Here, friend function addFive() is declared inside Distance class. So, the private data metercan be accessed from this function.
Though this example gives you an idea about the concept of a friend function, it doesn't show any meaningful use.
A more meaningful use would to when you need to operate on objects of two different classes. That's when the friend function can be very helpful.
You can definitely operate on two objects of different classes without using the friend function but the program will be long, complex and hard to understand.

friend Class in C++ Programming

Similarly, like a friend function, a class can also be made a friend of another class using keyword friend. For example:
... .. ...
class B;
class A
{
   // class B is a friend class of class A
   friend class B;
   ... .. ...
}

class B
{
   ... .. ...
}
When a class is made a friend class, all the member functions of that class becomes friend functions.
In this program, all member functions of class B will be friend functions of class A. Thus, any member function of class B can access the private and protected data of class A. But, member functions of class A cannot access the data of class B.
Remember, friend relation in C++ is only granted, not taken.

Comments