friend Function and friend Classes in C++

friend Function and friend Classes in C++



friend Function and friend Classes in C++
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 the programmer to write long and complex codes. So, there is a 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

  1. /* C++ program to demonstrate the working of friend function.*/
  2. #include <iostream>
  3. using namespace std;
  4. class Distance
  5. {
  6. private:
  7. int meter;
  8. public:
  9. Distance(): meter(0) { }
  10. //friend function
  11. friend int addFive(Distance);
  12. };
  13. // friend function definition
  14. int addFive(Distance d)
  15. {
  16. //accessing private data from non-member function
  17. d.meter += 5;
  18. return d.meter;
  19. }
  20. int main()
  21. {
  22. Distance D;
  23. cout<<"Distance: "<< addFive(D);
  24. return 0;
  25. }
Output
Distance: 5
Here, friend function addFive() is declared inside Distance class. So, the private data meter can 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.

Example 2: Addition of members of two different classes using friend Function

  1. #include <iostream>
  2. using namespace std;
  3. // forward declaration
  4. class B;
  5. class A {
  6. private:
  7. int numA;
  8. public:
  9. A(): numA(12) { }
  10. // friend function declaration
  11. friend int add(A, B);
  12. };
  13. class B {
  14. private:
  15. int numB;
  16. public:
  17. B(): numB(1) { }
  18. // friend function declaration
  19. friend int add(A , B);
  20. };
  21. // Function add() is the friend function of classes A and B
  22. // that accesses the member variables numA and numB
  23. int add(A objectA, B objectB)
  24. {
  25. return (objectA.numA + objectB.numB);
  26. }
  27. int main()
  28. {
  29. A objectA;
  30. B objectB;
  31. cout<<"Sum: "<< add(objectA, objectB);
  32. return 0;
  33. }
Output
Sum: 13
In this program, classes A and B have declared add() as a friend function. Thus, this function can access private data of both class.
Here, add() function adds the private data numA and numB of two objects objectA and objectB, and returns it to the main function.
To make this program work properly, a forward declaration of a class class B should be made as shown in the above example.
This is because class B is referenced within the class A using code: friend int add(A , B);.

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.

Comments

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website