Friend class and more

In this Page we will discuss about two following topics-


a Friend function more then one class

A friend function can be a friend of more than one class. 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 syntex, 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
};

Here is the complete program-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

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 f1 , second s1)           // defination of friend function
{
   int sum;
   sum = f1.a + s1.b;         // accessig private member of classes
   cout<<"Total: "<<sum;
} 

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

   first f2;
   second s2;

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

   f 2.get_num(a);           // first class function call
   s2.get_num(b);           // second class function call
   show_sum(f2,s2);        // friend function call

   getch();
}
OUTPUT:-
Enter two number: 5 6
Total: 11

 


Friend class

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 a class is a friend of many classes but it has some conditions like

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, one 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, so you will be able to understand it easily.

In the below diagram, we declare two class first and second, where we access first class private member a using first class object f1 to the  second class,

friend-class

Here is the Program,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

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 f1) // second class member defination
   {
     int sum;
     sum = f1.a + b; // accessing first class member using first class object f1
     cout<<"Total: "<<sum;
   }
};   // second class terminate

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

  first f2;            // first class object declare
  second s;        // second class object declare

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

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

 getch();
}
OUTPUT:-
Enter two number: 3 8
Total: 11

exaplanation

In this, the second class is a friend of the first class, so the member of the second class void get_sum(); can access the private member of the first class data member a (from the object of the first class, which declare as the parameter of that member) whereas the first class can access any of the second class Cannot access member.

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 | Friend Class

next-Constructor in C++