The central idea of encapsulation and data hiding concept is that any non-member function has no access permission to the private data of the class. The private members of the class are accessed only from member functions of that class.
C++ allows a mechanism, in which a non-member function has access permission to the private members of the class. This can be done by declaring a non-member function friend to the class whose private data is to be accessed. The friend is a keyword. Consider the following example.
The keyword friend must precede the function declaration, whereas function declarator must not. The function can be defined at anyplace in the program like normal function. The function can be declared as friend function in one or more classes. The keyword friend or scope access operator must not precede the definition of the friend function. The declaration of friend function is done inside the class in private or public part and a function can be declared as friend function in any number of classes. These functions use objects as arguments. Thus the following statement is wrong.
The above declaration of function is wrong because the function declarator precedes the keyword friend.
The friend functions have the following properties:
- There is no scope restriction for the friend function; hence, they can be called directly without using objects.
- Unlike member functions of class, the friend cannot access the member directly. On the other hand it uses object and dot operator to access the private and public member variables of the class.
- By default, friendship is not shared (mutual). For example, if class X is declared as friend of Y, this does not meant that Y has privileges to access private members of class X.
- Use of friend functions is rare, since it violates the rule of encapsulation and data hiding.
- The function can be declared in public or private sections without changing its meaning.
8.24 Write a program to access private data using non-member function. Use friend function.
Explanation: In the above program, class ac is declared. It has three member variables and one member function. Also, inside the class ac, showbal() is a function, which is declared as friend of the class ac. Once the outside function is declared as friend to any class, it gets an authority to access the private data of that class. The function read() reads the data through the keyboard such as name, account number, and balance. The friend function showbal() display the balance and acno.
8.25 Write a program to declare friend function in two classes. Calculate the sum of integers of both the classes using friend sum() function.
Explanation: In the above program, two classes first and second are declared with one integer and one member function in each. The member function getvalue() of both classes reads integers through the keyboard. In both the classes the function sum() is declared as friend. Hence, this function has an access to the members of both the classes. Using sum(), function addition of integers is calculated and displayed.
8.26 Write a program to exchange values between two classes. Use friend functions.
Enter value of j : 4
Enter value of k : 8
After change values are
Value of J = 8
Value of K = 4
Explanation: In the above program, two classes first and second are defined. Each class contains one integer variable and two member functions. The function input() is used to read an integer through the keyboard. The function show() is used to display the integer on the screen. The function change() is declared as friend function for both the classes. Passing values by reference of member variables of both the classes, values are exchanged.
8.27 Write a program to declare three classes. Declare integer array as data member in each class. Perform addition of two data member array into array of third class. Use friend function.
Explanation: In the above program, three classes A, B, and C are declared. Each class contains single integer arrays as data member a, b, and c, respectively. The class A and B contains member function input() to read integers. The function sum() is declared as friend in all the three classes. This function performs addition of arrays of class A and B and stores results in the array of class C. The result obtained is returned in main() where the return value is assigned to object c. In main() a, b, and c are objects of classes A, B, and C, respectively. The member function show() of class C displays the contents of object c.
It is possible to declare one or more functions as friend functions or an entire class can also be declared as friend class. When all the functions need to access another class in such a situation we can declare an entire class as friend class. The friend is not transferable or inheritable from one class to another. Declaring class A to be a friend of class B does not meant class B a friend of class A; that is, friendship is not exchangeable. The friend classes are applicable when we want to make available private data of a class to another class.
8.28 Write a program to declare friend classes and access the private data.
Explanation: In the above program, two classes A and B are declared. The class A is friend of class B. The member function of class A can access the data of class B. Thus, the show() function displays the values of data members of both the classes.
8.29 Write a program to demonstrate friend classes.
Explanation: In the above program, class C and class CPP are declared as friend. The class CPP is declared as friend class of class C. Member function of class CPP can access the private data variables of the class C.