Friend class in C++ with examples

In this Page, we will discuss two following concept,


It is like a friend function, it is just a class at the place of the function. in this, a class is a friend of another class or classes is a friend of

many classes where one class can access private members of another class,

here is an example of two-class First and Second are friend where Second class can access First-class Private members,

but there are some conditions, if the first class is a friend of the second class, then the first-class can access the private-member of the second class, but the second class cannot access the member of the first class. In a way, we can say that friendship is one-sided.

Let us consider this as the help of a program-

Here the above program has been given the form of friend class in C++, so you will be able to understand it easily.

In the below diagram, we declare two class First and class Second, where we access first-class private member using first-class object obj1 to the second class,

friend-Class-example-in-cppHere is the Program,

#include<iostream>
#include<stdio.h>
using namespace std;

 class First
  {
     int a;
     friend class Second; //second is declaration in private mode of first class
   public:
     void get_num(int x)
       {
          a = x;
       }
  }; // first class terminate

 class Second
  {
     int b;
    public:
      void get_num(int y)
       {
          b = y;
       }

   void get_sum( First obj1) // second class member defination
     {
       int sum;
       sum = obj1.a + b; // accessing first class member using first class object f1
       cout<<"Total: "<<sum;
     }
  };   // second class terminate

int main() // main function start here
 {
   int a,b;
   first obj2;            // first class object declare
   second s;        // second class object declare

   cout<<"Enter two number: ";
   cin>>a>>b;

   obj2.get_num(a);          // first class function call
   s.get_num(b);         // second class function call
   s.get_sum(obj2);        // friend function call

return 0;
}

OUTPUT

Enter two number: 3 8
Total: 11

Explanation

see the above diagram

a Friend function can friend more than one class

A friend function can be a friend of more than one class in C++. In such a situation, we have to forward the declaration of the class whose friend it is.

In this, both class names are declared as parameters of friend function in the declaration of friend function for example-

friend return-type function_name(first-class,second-class);

whereas the object of those classes is also declared in the definition of these friend functions-

return-type function_name(first-class obj1, second-class ob2)
  {
    ..............
    ..............
  };

In below syntax, just as friend function can declare both private and public, so here we have declared friend function in first-class in private and second class in public mode.

class second; // forward declaration
class first
  {
     data member;
     friend return type function_name(first, second); //private mode
   public:
      data member
      member function;
  };

class second
  {
     data member;
     member function;
   public:
     data member;
     member function;
     friend return type function_name(first, second); // public mode
  };

Remember in the two classes, the name of the declare friend function will be same while declaration anywhere (private or public mode) just like below where are void friend show_sum(first, second) is a friend type function,

class second; // forward declaration

class first
 {
     int a;
     void friend show_sum(first, second); // declared in private
   public:
      get_num(int x) { 
                  statement;
                  }
 };

class second
 {
     int b;
   public:
     void get_num(int y) { 
                 statement;
            }
    void friend show_sum(first, second); // declared in public
 };

According to this syntax, its program is given below-

#include<iostream>
#include<stdio.h>
using namespace std;

 class second; // forward declaration

 class first
   {
      int a;
      void friend show_sum(first,second);       // friend function declaration in private mode
    public:
      void get_num(int x)
        {
           a = x; 
        }
   };            // first class terminate here

 class second
   {
      int b;
     public:
      void get_num(int y)
        {
           b = y; 
        }
      void friend show_sum(first,second);          //friend declaration in public mode
   };              // second terminate here

void show_sum(first obj1 , second obj2)           // defination of friend function
  {
     int sum;
     sum = obj1.a + obj2.b;         // accessig private member of classes
     cout<<"Total: "<<sum;
  } 

int main()         // main function start here
{
   int a,b;
   clrscr();

   first obj1;
   second obj2;

   cout<<"Enter two number: ";
   cin>>a>>b;

   obj1.get_num(a);           // first class function call
   obj2.get_num(b);           // second class function call
   show_sum(obj1,obj2);        // friend function call

return 0;
}

OUTPUT

Enter two number: 5 6
Total: 11

Explanation

in the outside definition (outside of class) of a friend function, the scope resolution operator is not used, whereas a simple public member’s scope resolution operator is used in the outside definition.


Previous- Friend Function in C++
Next- constructor in C++


Like it?

Leave a Reply

Your email address will not be published.