Friend Function in C++

 


Friend Function:  It is a non member function that is used to access the protected and private data of class.It is used inside the class to access the private and protected data of class.

 1.. Program to find data of two different classes using friend function.

#include <iostream.h>

#include <conio.h>

class One; //forward declaration

class Two

{

private:

      int num2;

public:

       void input();

        void display();

        friend void maximum(One a, Two b);


          };

class One

     {

     private:

            int num1;

     public:

             void read();

             void show();

             friend void maximum(One a, Two b);

                    };

void One:: read()

      {

         cout<<"Enter a number for class One";

           cin>>num1;

             }

void One::show()

      {

         cout<<endl<<" Value of num in class One is "<<num1<<endl;

             }

void Two:: input()

       {

          cout<<"Enter a number for class Two";

             cin>>num2;

                 }

void Two:: display()

     {

        cout<<endl<<"Value of num in class Two is "<<num2<<endl;

           }

void maximum(One x, Two y)

     {

     if(x.num1>y.num2)

       cout<<x.num1<<" is greater than "<<y.num2;

     else

        cout<<y.num2<<" is greater than "<<x.num1;

          }

void main()

  {

 One a;

 Two b;

 a.read();

 cout<<endl;

 b.input();

 cout<<endl;

 a.show();

 b.display();

 cout<<endl;

 maximum(a,b);

   getch();

   }

Output:


2. Program to swap the private data of two classes using  friend function using

a.  Using member function of one class as friend of another class


#include<iostream.h>

#include<conio.h>

class one;

class two

{

private:

int num2;

 public:

 void setNum();

 void displayNum();

  void swap(one &a);

 };

 class one

 {

 private:

 int num1;

 public:

 void setNum();

 void displayNum();

 friend void two::swap(one &a);

 };

 void one:: setNum()

 {

 cout<<"x=:";

 cin>>num1;

 }

 void one:: displayNum()

 {

 cout<<num1<<endl;

  }

  void two:: setNum()

  {

 cout<<"y= :";

 cin>>num2;

 }

 void two:: displayNum()

 {

 cout<<num2<<endl;

  }

  void two::swap(one &x)

 {

 int temp;

 temp = x.num1;

x.num1 =num2;

 num2 = temp;

 }

void main()

{

  one a;

  two b;

  cout<<"enter the value ";

  a.setNum();

   cout<<"enter the value ";

  b.setNum();

  cout<<"value before swapping= ";

  a.displayNum();

  cout<<"value before swapping= ";

  b.displayNum();

  cout<<endl;

  b.swap(a);

  cout<<"The value of 'x' after swapping is :";

  a.displayNum();

 cout<<"The value of 'y' after swapping is :";

  b.displayNum();

  getch();

  }

Output:


b. Global function as friend

#include<iostream.h>

#include<conio.h>

class One;

class Two

{

private:

int num2;

 public:

 void setNum();

 void displayNum();

 friend void swap(One &x,Two &y);

 };

 class One

 {

 private:

 int num1;

 public:

 void setNum();

 void displayNum();

 friend void swap(One &x,Two &y);

 };

 void One:: setNum()

 {

 cout<<"Enter first Value:";

 cin>>num1;

 }

 void One:: displayNum()

 {

 cout<<num1<<endl;

  }

  void Two:: setNum()

  {

 cout<<"Enter second value:";

 cin>>num2;

 }

 void Two:: displayNum()

 {

 cout<<num2<<endl;

  }

  void swap(One &x,Two &y)

 {

 int temp;

 temp = x.num1;

 x.num1 = y.num2;

 y.num2 = temp;

 }

 void main()

 {

  One a;

  Two b;

  a.setNum();

  b.setNum();

    cout<<endl;

  cout<<"Value before swapping:";

  a.displayNum();

  cout<<"Value before swapping:";

  b.displayNum();

  cout<<endl;

  swap(a,b);

  cout<<"Value after swapping  :";

  a.displayNum();

cout<<"Value after swapping :";

  b.displayNum();

  getch();

  }

Output:


c. Friend Class


#include <iostream.h>
#include <conio.h>
class one;
class two
{
private:
int num2;
 public:
 void setNum();
 void displayNum();
  void swap(one &a);
 };
 class one
 {
 private:
 int num1;
 public:
 void setNum();
 void displayNum();
 friend void two::swap(one &a);
 };

 void one:: setNum()
 {
 cout<<"x=:";
 cin>>num1;
 }
 void one:: displayNum()
 {
 cout<<num1<<endl;
  }

  void two:: setNum()
  {

 cout<<"y= :";
 cin>>num2;
 }
 void two:: displayNum()
 {
 cout<<num2<<endl;
  }


  void two::swap(one &a)
 {
 int temp;
 temp = a.num1;
a.num1 =num2;
 num2 = temp;
 }
void main()
{
  one a;
  two b;
  cout<<"enter the value ";
  a.setNum();

   cout<<"enter the value ";
  b.setNum();

  cout<<"value before swapping:";
  a.displayNum();

  cout<<"value before swapping:";
  b.displayNum();

  cout<<endl;
  b.swap(a);

  cout<<"The value of 'x' after swapping is :";
  a.displayNum();

  cout<<"The value of 'y' after swapping is :";
  b.displayNum();
  getch();
  }
Output:


3. Program to add two complex number by using friend function
 

#include<iostream.h>
#include<conio.h>
class Complex
{
private:
int real,img;

public:
void getdetails();
void display();
friend void Com(Complex C1,Complex C2);
};
void Complex::getdetails()
{
cout<<"Real Part :";
cin>>real;
cout<<"Imaginary part :";
cin>>img;
}
void Complex::display()
{
if(img>0)
{

cout<<real<<"+j"<<img;
}
else
{
cout<<real<<"j"<<(-1)*img;
}
    }

void Com(Complex C1,Complex C2)
{
Complex C3;
  C3.real=C1.real+C2.real;
  C3.img=C1.img+C2.img;
     C3.display();
  }
  void main()
  {
  Complex C1,C2;
  cout<<"enter the first complex number:"<<endl;
  C1.getdetails();
  cout<<"enter the second complex number:"<<endl;
C2.getdetails();
  cout<<"First complec number is :"<<endl;
  C1.display();
  cout<<endl;
  cout<<"Second complex number is :"<<endl;
  C2.display();
cout<<"The Sum is :";
  Com(C1,C2);
getch();
  }
Output:



4. Write a program to define class Distance
with data members feet and inches of appropriate type.
Define friend functionwhich accepts two objects
of Distance and adds them.

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

class Distance
{
private:
int feet,inches;
public:
void getdetails();
void display();
friend void add(Distance d1,Distance d2);
};
void Distance::getdetails()
{
cout<<"Enter the value of feet:";
cin>>feet;
cout<<"Enter the value of inches:";
cin>>inches;
}
void Distance::display()
{
cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
}
void add(Distance d1,Distance d2)
{
Distance d3;
int inches1;
d3.inches=d1.inches + d2.inches;
inches1=d3.inches/12;
d3.inches=d3.inches%12;
d3.feet=inches1+ d1.feet + d2.feet;

d3.display();
}
void main()
{
Distance D1,D2;
cout<<"Enter the first value for distance:"<<endl;

D1.getdetails();
cout<<endl;
cout<<"Enter the second value for distance:"<<endl;
D2.getdetails();
cout<<endl;
cout<<"The first value of distance is:"<<endl;
D1.display();
cout<<endl;
cout<<"the second value of distance is:"<<endl;
D2.display();
cout<<endl;
cout<<"The distance after addition is :"<<endl;
add(D1,D2);

getch();
 }
Output:

5. Write a program to define class time with data members
 hour,minute,second, define a friend function to add two times .


#include<iostream.h>
#include<conio.h>
class Time
{
private:
int hr,min,sec;

public:
void getdetails();
void display();
friend void result(Time t1,Time t2);
};
void Time::getdetails()
{
cout<<"Enter the hr :";
cin>>hr;
cout<<"Enter the min :";
cin>>min;
cout<<"Enter the sec :";
cin>>sec;
}
void Time::display()
{
cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
}
void result(Time t1,Time t2)
{
Time t3;
t3.sec=t1.sec+t2.sec;
t3.min=t3.sec/60;
t3.sec=t3.sec%60;
t3.min=t1.min+t2.min;
t3.hr=t3.min/60;
t3.min=t3.min%60;

t3.hr=t1.hr+t2.hr+t3.hr;

t3.display();
}

void main()
{
Time t1,t2;
cout<<"Enter the  first time :"<<endl;
t1.getdetails();
cout<<endl;
cout<<"Enter the second time :"<<endl;
t2.getdetails();
cout<<endl;
cout<<"The first time is:"<<endl;
t1.display();
cout<<endl;
cout<<"the second time is:"<<endl;
t2.display();

cout<<endl;
cout<<"The resultant time after addition is :"<<endl;
 result(t1,t2);
  getch();
 }
Output:

6. Create a class called Mountain with data members name,height and location. 
Define a member function to initialize the data members.
A friend function cmpHeight() to compare height
of two objects and member function displayInfo()
to display information of mountain.
Create two objects of the class mountain and display the information of mountain with greatest height.

#include<iostream.h>
#include<conio.h>
class mountain
{
private:
      char name[20];
      char location[20];
      int height;
public:
       void getdetails();
       void display();
        friend void compht( mountain m1,mountain m2);
        };
void mountain::getdetails()
{
   cout<<"Enter the name of mountain:";
    cin>>name;
     cout<<"Enter the location of mountain:";
     cin>>location;
    cout<<"Enter the height of mountain:";
      cin>>height;
        }
void mountain::display()
{
cout<<"Name of mountain:"<<name;
cout<<endl;
cout<<"Location of mountain:"<<location;
cout<<endl;
cout<<"Height of mountain:"<<height;
cout<<endl;
     }
void compht(mountain m1,mountain m2)
{
if(m1.height>m2.height)
  {
   m1.display();
     }
      else if (m2.height>m1.height)
       {
        m2.display();
         }
          else
             {
              cout<<"Both mountains are of equal height";
                 }
                   }
void main()
{
 mountain M1,M2;
 M1.getdetails();
 cout<<endl;
 M2.getdetails();
 cout<<endl;
 M1.display();
 cout<<endl;
 M2.display();
 cout<<endl;
 cout<<"The details of highest mountain:"<<endl;
 compht(M1,M2);
 getch();
 }
Output:



7. Write a program which has two classes named Practical and
Theory, which have data members to store practical and theory marks
respectively. Define a class Marks that has member function to
calculate total marks ( i.e practical marks of Practical class plus theory
marks of Theory class ). Use the concept “member function of one class
is friend of another class”.

#include<iostream.h>
#include<conio.h>
class theory;
class practical;
class marks
{
public:
void total(practical P,theory T);
};
  class practical
  {
private:
int phy,chem;
 public:
 void setmarks();
 void displaymarks();
 friend void marks::total(practical P,theory T);
 };
 class theory
 {
 private:
 int phy,chem;
 public:
 void setmarks();
 void displaymarks();
 friend void marks::total(practical P,theory T);
 };

  void theory:: setmarks()
  {
 cout<<"Enter the theory marks of physic:";
 cin>>phy;
 cout<<endl;
 cout<<"Enter the theory marks of Chemistry:";
 cin>>chem;
}
            void theory::displaymarks()
            {
            cout<<endl;
            cout<<" The entered theory marks of Physic:"<<phy<<endl<<endl<<"The entered theory marks of Chemistry:"<<chem<<endl<<endl;
            }

 void practical::setmarks()
 {
 cout<<endl;
 cout<<"Enter the practical marks of Physics:";
 cin>>phy;
 cout<<endl;
 cout<<"Enter the practical marks of Chemistry:";
 cin>>chem;

 }
 void practical:: displaymarks()
 {
 cout<<endl;
 cout<<"The entered practical marks of physic:"<<phy<<endl<<endl<<"The entered practical marks of chemistry:"<<chem<<endl<<endl;
  }

  void marks::total(practical a,theory b)
 {
int ptotal,ttotal;
ptotal=a.phy+a.chem;
ttotal=b.phy+b.chem;
 cout<<"Total marks:"<<endl<<endl;
  cout<<"The total theory marks of physic and chemistry :"<<ttotal<<endl;
cout<<endl;
  cout<<"The total practical marks of physic and chemistry :"<<ptotal<<endl;
     }

void main()
{
  practical p;
  theory th;
  marks t;
  th.setmarks();
  p.setmarks();
  th.displaymarks();
  p.displaymarks();
      t.total(p,th);

  getch();
  }
Output:


8. . Create classes called Class1,Class2, Class3, Class4 with each having one private member.
 Add member function to set a value (say setValue) on each class.
Add one more function max() that is friendly to allclasses, max() function should compare
four private member of fourclasses and show maximum among them.
Create one-one object of each class and set a value on them.
Display the maximum number among them. 

#include<iostream.h>
#include<conio.h>
class Class1;
class Class2;
class Class3;
class Class4
{
private:
int num;
public:
void setvalue();
friend void max(Class1 & ,Class2 & ,Class3 & , Class4 &);
};
void Class4:: setvalue()
{
cout<<"Enter value of num:";
cin>>num;
}
class Class1
{
private:
int num;
public:
void setvalue();
friend void max(Class1 & ,Class2 & ,Class3 & , Class4 &);

};
void Class1::setvalue()
{
cout<<"Enter value of num:";
cin>>num;
}
class Class2
{
private:

int num;

public:
void setvalue();
friend void max(Class1 & ,Class2 & ,Class3 & , Class4 &);
};

void Class2::setvalue()
{
cout<<"Enter value of num:";
cin>>num;
}
class Class3
{
private:
int num;
public:
void setvalue();

friend void max(Class1 & ,Class2 & ,Class3 & , Class4

&);
};
void Class3::setvalue()
{
cout<<"Enter value of num:";
cin>>num;
}
void max(Class1 &c1,Class2 &c2,Class3 &c3, Class4 &c4 )
{
if(c1.num>c2.num && c1.num>c3.num && c1.num>c4.num)

{
cout<<c1.num<<"is maximum ";
}
else if (c2.num>c3.num && c2.num>c4.num)
{
cout<<c2.num<<"is maximum";

}
else if(c3.num>c4.num)
{
cout<<c3.num<<"is maximum";
}

else
{
cout<<c4.num<<"is maximum";
}
}
void main()
{
Class1 c1;
Class2 c2;
Class3 c3;
Class4 c4;
cout<<"For Class C1:"<<endl;
c1.setvalue();
cout<<endl;
cout<<"For Class C2:"<<endl;
c2.setvalue();
cout<<endl;
cout<<"For Class C3:"<<endl;
c3.setvalue();
cout<<endl;
cout<<"For Class C4:"<<endl;
c4.setvalue();
cout<<endl;
max(c1,c2,c3,c4);
getch();
}
Output

Return Type


 9.Write a program to define class time with data members
 hour,minute,second, define a friend function to add two time and return them.

#include<iostream.h>
#include<conio.h>
class Time
{
private:
int hr,min,sec;

public:
void getdetails();
void display();
 Time result(Time,Time);
};
void Time::getdetails()
{
cout<<"Enter the hr :";
cin>>hr;
cout<<"Enter the min :";
cin>>min;
cout<<"Enter the sec :";
cin>>sec;
}
void Time::display()
{
cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
}
Time Time:: result(Time t1,Time t2)
{
Time t3;
t3.sec=t1.sec+t2.sec;
t3.min=t3.sec/60;
t3.sec=t3.sec%60;

t3.min=t1.min+t2.min;
t3.hr=t3.min/60;
t3.min=t3.min%60;

t3.hr=t1.hr+t2.hr+t3.hr;
return t3;
}

void main()
{
Time t1,t2,t3;
cout<<"Enter the  first time :"<<endl;
t1.getdetails();
cout<<endl;
cout<<"Enter the second time :"<<endl;
t2.getdetails();
cout<<endl;
cout<<"The first time is:"<<endl;
t1.display();
cout<<endl;
cout<<"the second time is:"<<endl;
t2.display();

cout<<endl;
 t3=t3.result(t1,t2);

 cout<<"The resultant time after addition is :"<<endl;
 t3.display();
 getch();
 }
Output:

/* 
#include<iostream.h>
#include<conio.h>
class Time
{
private:
int hr,min,sec;

public:
void getdetails();
void display();
 friend Time result(Time,Time);
};
void Time::getdetails()
{
cout<<"Enter the hr :";
cin>>hr;
cout<<"Enter the min :";
cin>>min;
cout<<"Enter the sec :";
cin>>sec;
}
void Time::display()
{
cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
}
Time result(Time t1,Time t2)
{
Time t3;
t3.sec=t1.sec+t2.sec;
t3.min=t3.sec/60;
t3.sec=t3.sec%60;

t3.min=t1.min+t2.min;
t3.hr=t3.min/60;
t3.min=t3.min%60;

t3.hr=t1.hr+t2.hr+t3.hr;
cout<<"Sum of time is :";
t3.display();

}

void main()
{
Time t1,t2;
cout<<"Enter the  first time :"<<endl;
t1.getdetails();
cout<<endl;
cout<<"Enter the second time :"<<endl;
t2.getdetails();
cout<<endl;
cout<<"The first time is:"<<endl;
t1.display();
cout<<endl;
cout<<"the second time is:"<<endl;
t2.display();

cout<<endl;
 result(t1,t2);
getch();
 }
*/


10.. Write a program to define class Distance with data members feet and inches 
of appropriate type.
Define friend function which accepts two objects
of Distance and adds them and return them..

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

class Distance
{
private:
int feet,inches;
public:
void getdetails();
void display();
 Distance add(Distance d1,Distance d2);
};
void Distance::getdetails()
{
cout<<"Enter the value of feet:";
cin>>feet;
cout<<"Enter the value of inches:";
cin>>inches;
}
void Distance::display()
{
cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
}
Distance Distance:: add(Distance d1,Distance d2)
{
Distance d3;
int inches1;
d3.inches=d1.inches + d2.inches;
inches1=d3.inches/12;
d3.inches=d3.inches%12;
d3.feet=inches1+ d1.feet + d2.feet;
return d3;
}
void main()
{
Distance D1,D2,D3;
cout<<"Enter the first value for distance:"<<endl;

D1.getdetails();
cout<<endl;
cout<<"Enter the second value for distance:"<<endl;
D2.getdetails();
cout<<endl;
cout<<"The first value of distance is:"<<endl;
D1.display();
cout<<endl;
cout<<"the second value of distance is:"<<endl;
D2.display();
cout<<endl;
D3=D3.add(D1,D2);
 cout<<"The sum of two distance is:";
 D3.display();
getch();
 }
Output:


/* 
#include<iostream.h>
#include<conio.h>
class Time
{
private:
int hr,min,sec;

public:
void getdetails();
void display();
friend Time result(Time t1,Time t2);
};
void Time::getdetails()
{
cout<<"Enter the hr :";
cin>>hr;
cout<<"Enter the min :";
cin>>min;
cout<<"Enter the sec :";
cin>>sec;
}
void Time::display()
{
cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
}
 Time result(Time t1,Time t2)
{
Time t3;
t3.sec=t1.sec+t2.sec;
t3.min=t3.sec/60;
t3.sec=t3.sec%60;

t3.min=t1.min+t2.min;
t3.hr=t3.min/60;
t3.min=t3.min%60;

t3.hr=t1.hr+t2.hr+t3.hr;
cout<<"The sum is:";
t3.display();
}

void main()
{
Time t1,t2;
cout<<"Enter the  first time :"<<endl;
t1.getdetails();
cout<<endl;
cout<<"Enter the second time :"<<endl;
t2.getdetails();
cout<<endl;
cout<<"The first time is:"<<endl;
t1.display();
cout<<endl;
cout<<"the second time is:"<<endl;
t2.display();

cout<<endl;
 result(t1,t2);
 getch();
 }
*/

11.Create a class called Complex and add the real and imaginary value of two classes using friend function and return them.
 

#include<iostream.h>
#include<conio.h>
class Complex
{
private:
int real,img;

public:
void getdetails();
void display();
Complex Com(Complex C1,Complex C2);
};
void Complex::getdetails()
{
cout<<"Real Part :";
cin>>real;
cout<<"Imaginary part :";
cin>>img;
}
void Complex::display()
{
if(img>0)
{

cout<<real<<"+j"<<img;
}
else
{
cout<<real<<"j"<<(-1)*img;
}

}
Complex Complex::Com(Complex C1,Complex C2)
{
Complex C3;
  C3.real=C1.real+C2.real;
  C3.img=C1.img+C2.img;
     return C3;
  }
  void main()
  {
  Complex C1,C2,C3;
  cout<<"enter the first complex number:"<<endl;
  C1.getdetails();
  cout<<endl;
  cout<<"enter the second complex number:"<<endl;
C2.getdetails();
cout<<endl;
  cout<<"First complec number is :"<<endl;
  C1.display();
  cout<<endl;
  cout<<"Second complex number is :"<<endl;
  C2.display();

  cout<<endl<<"The sum is :"<<endl;
  C3=C3.Com(C1,C2);
C3.display();
  getch();
  }
Output:


/* 

#include<iostream.h>
#include<conio.h>
class Complex
{
private:
int real,img;

public:
void getdetails();
void display();
friend Complex Com(Complex C1,Complex C2);
};
void Complex::getdetails()
{
cout<<"Real Part :";
cin>>real;
cout<<"Imaginary part :";
cin>>img;
}
void Complex::display()
{
if(img>0)
{

cout<<real<<"+j"<<img;
}
else
{
cout<<real<<"j"<<(-1)*img;
}

}
Complex Com(Complex C1,Complex C2)
{
Complex C3;
  C3.real=C1.real+C2.real;
  C3.img=C1.img+C2.img;
  cout<<"Sum is:";

   C3.display();
  }
  void main()
  {
  Complex C1,C2;
  cout<<"enter the first complex number:"<<endl;
  C1.getdetails();
  cout<<endl;
  cout<<"enter the second complex number:"<<endl;
C2.getdetails();
cout<<endl;
  cout<<"First complec number is :"<<endl;
  C1.display();
  cout<<endl;
  cout<<"Second complex number is :"<<endl;
  cout<<endl;
  C2.display();
Com(C1,C2);

  getch();
  } 
*/

Comments