C++ Programming | Friend Function

Friend functions/Bridge Function

Whenever data variables are declared in the private category of a class, these members are restricted from accessing by non-member functions. Any non-member function has no access permission to the private data of the class. The private data values can neither be read nor written by non-member function. If any attempt is made directly to access these members, the compiler generates an error as an “inaccessible data type”. The private members of the class are accessed only from member functions of that class. But the best way to access a private data member by a non-member function is to change a private data member to a public group. When the private or protected data member is changed to the public category, it violates the whole concept of data hiding and data encapsulation.

To solve this problem, it is possible to grant access to a non-member function, for private and protected members of a class by using a “friend” keyword while declaring the function.

“Friend function is a special function which has the capability to access the private data elements of two or more objects, the objects may belong to the same or different classes.”

Syntax:

friend return_type function_name(parameters);

Properties
1. A friend function is declared using the keyword “friend”.
2. Friend function has to be declared in all the classes whose private data elements are to be accessed.
3. The declaration of friend function in each class whose private data elements are to be accessed has to be exactly the same.
4. There will be a common definition of the friend function and it is shared by all the objects.
5. While giving the definition of friend function keyword “friend” is not used.
6. While giving the definition of friend function the reference of a class is not given.
7. Within the definition of friend function, the data elements of the object are accessed using dot (.) operator.
8. While calling a friend function we do not require an object of the class. i.e. a friend function is invoked without using as an object.
9. There can be any number of friend functions in a class.

A function can be friend to multiple classes.

Question : Friend function is not a member function. Justify?

Ans:

• 6. While giving the definition of friend function the reference of class is not given.
• 8. While calling a friend function we do not require an object of the class. i.e. a friend function is invoked without using an object.

Merits/advantages/benefits

1. Private data of other classes can be accessed using friend functions.
2. The scope of friend function is not limited to the class in which it is declared.
3. It can be invoked like a normal function without the help of the object and dot operator.
4. A friend function can be used to increase the versatility of the overloaded operators.
5. The values of the private members of a class can be altered by friend function.
6. The friend function is used to bridge, classes when the function operates on the objects of two different classes.

Demerits/disadvantages

1. Each time a friend function access the private data, naturally the level of privacy of the data encapsulation gets reduced.
2. Friend function is not in the scope of the class to which it has been declared as friend.
3. It cannot be called with the object of a class, using a dot (.) operator.
4. In friend function, it is not possible to access the data elements directly.

Number Of Visitors

0238208