c++

Friend Function in C++

In Object-Oriented Programming, a friend Function which is a "friend" of a given class, is a special function that is given the same access as given to the methods to private data and protected data. For example a LinkedList class may be allowed to access private members of Node.

A friend Function is declared by the class that is granting access, so friend functions are part of the class interface. A friend function of a class is defined outside that class but it can access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.In a designed program the private and protected data can be accessed using the function ,if the function is defined as a friend function.The compiler can easily understand that the given function is a Friend Function ,the compiler can do so by seeing a keyword called friend.


Properties of friend function


Declaration of friend function

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

/* working of friend function.*/

#include <iostream> 

using namespace std;

class interspace {
    private:
        int centimeter;
    public:
        interspace(): centimeter(0) { }

        //friend function
        friend int addsixteen(interspace);
};

// friend function definition
int addsixteen(interspace i) {
    //accessing private data from non-member function
    i.centimeter += 16;
    return i.centimeter;
}

int main() {
    interspace I;

    cout<<"interspace: "<< addsixteen(I);

    return 0;
}

Output:

interspace: 16

Here, friend function addsixteen() is declared inside interspace class. So, the private data centimeter can be accessed from this function.A more meaningful use would to when you need to operate on objects of two different classes.

Example 2:Addition of member of two different classes using friend function

include <iostream>

using namespace std;

// forward declaration class P
class Q {
    private:
        int numberP;
    public:
        P(): numberP(25) { }

        // friend function declaration
        friend int add(P, Q);
};

class Q {
    private:
        int numberQ;
    public:
        Q(): number(8) { }
        // friend function declaration
        friend int add(P , Q);
};

// Function add() is the friend function of classes A and B
// that accesses the member variables numberP and numberQ 
int add(P objectP, Q objectQ) {
    return (objectP.numberP + objectQ.numberQ);
}

int main(){
    P objectP;
    Q objectQ;

    cout<<"Sum: "<< add(objectP, objectQ);

    return 0;
}

Output:

Sum: 33

the above program, classes P and class Q have declared add() as a friend function. Thus, this function can access private data of both the class.Here, add() function adds the private data numberP and numberQ of two objects objectP and objectQ , and returns it to the main function.