text: In this chapter we shall learn about below topics:
  - Friend Introduction.
  - Friend Function
  - Example 1: Program to add 2 using a friend function.
  - Example 2:
  - Friend Class
  1. Friend Function Introduction
  We know that C++ is a Object Oriented Programming language. It supports data hiding. i.e making data members private and then expose them using getters and setters.
  But sometimes we need to access the private members in other functions that is not a member function of that class. In such scenarios we use friend function and friend class.
  In this tutorial we shall see more about friend function and friend class with series of examples.
  2. Friend Function.
  A function which is not a part of the class, can be made to access the private data members of that class by declaring the function as a friend of that class.
  “friend” is the keyword used to declare a function as a friend of a class. Then you need to send the class object as one of the input arguments to make it work.
  Syntax:
  friend <return_type> <func_name> (<Class_Object>);
  Example 1: Program to add 2 using a friend function.
  #include <iostream> using namespace std; class MyClass { int a; public: void init(int num) { a = num; } friend void add_two(MyClass); }; void add_two(MyClass obj) { // we are able to access private data member "a" from MyClass function cout<<"Original value is" << obj.a<<endl; //we are able to modify the private data member "a" cout<<"Value after adding 2 is " << (obj.a+2)<<endl; } int main(void) { MyClass myObj; myObj.init(10); add_two(myObj); return 0; }
  Output:
  Original value is10 Value after adding 2 is 12
  Example 2:
  You can also make a single function as a friend of 2 different classes. In this example we shall see how to do it.
  #include <iostream> using namespace std; //forward declaration class MyClass_B; class MyClass_A { int a; public: void init(int num) { a = num; } friend void add_two(MyClass_A, MyClass_B); }; class MyClass_B { int b; public: void init(int num) { b = num; } friend void add_two(MyClass_A, MyClass_B); }; void add_two(MyClass_A obj_a, MyClass_B obj_b) { // we are able to access private data member "a" from MyClass_A function cout<<"Original value of A is " << obj_a.a<<endl; // we are able to access private data member "a" from MyClass_B function cout<<"Original value of B is " << obj_b.b<<endl; //adding a and b cout<<"Value after adding a and b is " << (obj_a.a + obj_b.b)<<endl; } int main(void) { MyClass_A obj_a; obj_a.init(10); MyClass_B obj_b; obj_b.init(20); add_two(obj_a, obj_b); return 0; }
  Output:
  Original value of A is 10 Original value of B is 20 Value after adding a and b is 30
  5. Friend Class.
  Similar to friend function, we can also make one class as a friend of another class.
  Example:
  We shall take input for a rectangle in ClassA and calculate the result and display the area in ClassB.
  #include <iostream> using namespace std; //forward declaration class MyClass_B; class MyClass_A { int length; int breadth; public: void init(int length, int breadth) { this-> length = length; this-> breadth = breadth; } friend MyClass_B; }; class MyClass_B { public: void calculate(MyClass_A obj) { // we are able to access MyClass_A private data memebers. cout<<"The area is "<<(obj.length * obj.breadth)<<endl; } }; int main(void) { MyClass_A obj_a; obj_a.init(10, 20); MyClass_B obj_b; obj_b.calculate(obj_a); return 0; }
  Output:
  The area is 200