Friend Function in C++

Friend Function in C++

1. Friend function is not a member function of a class to which it is a friend.
2. Friend function is declared inside the class with friend keyword.
3. It must be defined outside the class to which it is friend.
4. It can access any member of the class to which it is friend.
5. It cannot access members of the class directly.
6. It has no caller object.
7. It should not be defined with membership label (::).
8. Friend function can become friend to more than one class.

For Example:-
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  void setData(int num1, int num2)
  {
    real = num1;
    imaginary = num2;
  }
  
  void showData()
  {
    cout << real << " " << imaginary;
  }
  
  friend void func(Complex);       //declaration of friend function inside class.
};

void func(Complex C)              //definition of friend function outside class.
{
  cout << "Sum is: " << C.real+C.imaginary;
}

int main()
{
  Complex C1,C2,C3;
  C1.setData(11,25);
  
  func(C1);
}

Friend function can become friend to more than one class.
For example:-
#include<iostream>
using namespace std;

class B;                  //forward declaration of class B.

class A
{
private:
  int num1;
  
public:
  friend void fun(A, B);
  
  void setData(int x)
  {
    num1 = x;
  }
};

class B
{
private:
  int num2;
  
public:
  friend void fun(A, B);
  
  void setData(int y)
  {
    num2 = y;
  }
};

void fun(A o1, B o2)
{
  cout << "Sum is: " << o1.num1+o2.num2;
}

int main()
{
  A obj1;
  B obj2;
  
  obj1.setData(99);
  obj2.setData(11);
  
  fun(obj1, obj2);
}

Operator Overloading in C++ using friend function

The number of argument passed for operator overloading using friend function is one more that the number of arguments passed for operator overloading in member function.


For Example:-
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  void setData(int num1, int num2)
  {
    real = num1;
    imaginary = num2;
  }
  
  void showData()
  {
    cout << real << " " << imaginary;
  }
  
  friend Complex operator+(Complex, Complex);        //declaration of friend function inside class.
};

Complex operator+(Complex C1, Complex C2)           //definition of friend function outside class.
{
  Complex temp;
  
  temp.real = C1.real+C2.real;
  temp.imaginary = C1.imaginary+C2.imaginary;
  return temp;
}

int main()
{
  Complex C1,C2,C3;
  
  C1.setData(3,4);
  C2.setData(5,6);
  
  C3 = C1+C2;          //C3=operator+(C1,C2)
  C3.showData();
}

Overloading of unary operator using friend function

We can also overload any unary operator by using friend function. We need to declare friend function as operator overloading inside class and defining it outside the class.


For Example:-
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  void setData(int num1, int num2)
  {
    real = num1;
    imaginary = num2;
  }
  
  void showData()
  {
    cout<< real << " " << imaginary;
  }
  
  friend Complex operator-(Complex);
};

//Operator overloading of - symbol using friend function
Complex operator-(Complex C)
{
  Complex temp;
  
  temp.real = -C.real;
  temp.imaginary = -C.imaginary;
  return temp;
}

int main()
{
  Complex C1,C2;
  
  C1.setData(3,4);
  C2 = -C1;              //C2=operator-(C1);
  C2.showData();
}

Insertion and Extraction Operator Overloading using friend function


For Example:-
#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  void setData(int num1, int num2)
  {
    real = num1;
    imaginary = num2;
  }
  
  void showData()
  {
    cout << real << " " << imaginary;
  }
  
  friend ostream& operator << (ostream&, Complex);
  friend istream& operator >> (istream&, Complex);
};

ostream& operator << (ostream&dout, Complex C)
{
  cout << "\n real= " << C.real << "imaginary= " << C.imaginary;
  return dout;
}

istream& operator >> (istream&din, Complex &C)
{
  cin >> C.real >> C.imaginary;
  return din;
}

int main()
{
  Complex C1;
  
  cout << "Enter a Complex number:\n";
  cin >> C1;
  
  cout << "You Entered: ";
  cout << C1;                    //operator<<(cout, C1);
}

Member function of one class can become friend to another class.

For Example:-
#include<iostream>
using namespace std;

class A
{
public:
  void func1()
  {
    //...
  }
  
  void func2()
  {
    //...
  }
};

class B
{
  friend void A::func1();
  friend void A::func2();
};

If we want to make each and every member function of one class friend function of another class then use friend keyword before class and className in declaration of the friend function inside the class.


For Example:-
#include<iostream>
using namespace std;

class A
{
public:
  void func1()
  {
    //...
  }
  
  void func2()
  {
    //...
  }
};

class B
{
  //this will make func1 and func2 friend function to class B.
  friend class A;
};



<< Previous                                                                                      Next >>
Previous
Next Post »