• We all know that as per the rule of encapsulation, We cannot access data members or member functions declared with the private scope specifier from outside the class.
  • There are some cases in which we need to share some member function or variable between two or more classes.
  • Fortunately, C++ provides one feature called “friend function” that helps to achieve this goal.
  • A friend function works as common function for the classes in which we need to share their private data members among them.
  • Friend functions are just like normal function. We use the declaration of friend function within the classes, whose private member we want to access using friend function.
  • We just simply need to put the ‘friend’ keyword before the function declaration but note that function definition is written without the “friend” keyword.
  • Function definition of friend function is written outside the class.
  • Note that friend function is not a member of the class in which it is declare so its definition is written without the class name and scope resolution operator.

Properties of Friend Function

The following are the main properties of friend function:

  1. Its scope is not limited to the class in which it is declared.
  2. Friend function is called directly without using the object name (as it is not a member of a class).
  3. It cannot access the member of class directly but we can access the private and public member by using the object.
  4. Friend function can be declared at anywhere either in public or in private section. It doesn’t make any difference.
  5. It generally use object as an argument.
  • Let’s consider the below example program in which we are creating two classes ‘Fahrenheit’ and ‘Degree’. They are used for the temperature calculation.
  • Fahrenheit’ bill is calculated based on the no. of units and per unit charge whereas telephone bill is calculated based on no. of calls and per call charge.
  • We are using two friend function “exchange” which exchanges value of Fahrenheit and degree while “out”. friend function is used display data.
  • To use another class in function declaration we have to make forward declaration of class. So here we have did forward declaration of class Fahrenheit as its reference is required in Degree class.
/* program to convert Fahrenheit to degree and vise versa uing friend function */

#include "iostream.h"
#include "conio.h"
class Fahrenheit;  // forward declaration for class Fahrenheit
class Degree
{
	private:
		float d;
	public:
		Degree()
		{
			cout<<"Degree constructor called without parameter "<<endl;
			d=0.0;
		}

		Degree(float a)
		{
			cout<<"Degree constructor called "<<endl;
			d=a;
		}

		~Degree()
		{
			cout<<"Destructor called "<<endl;
		}

		private:
                       // friend function declaration. 
			friend void exchange(Degree &,Farenheit &);  
			friend void out(Degree &,Farenheit &);
};

class Fahrenheit
{
	private:
		float f;
	public:
		Fahrenheit()
		{
			cout<<"Fahrenheit constructor called without parameter "<<endl;
			f=0.0;
		}

		Fahrenheit(float a)
		{
			cout<<"Fahrenheit constructor called "<<endl;
			f=a;
		}

		~Fahrenheit()
		{
			cout<<"Fahrenheit destructor called "<<endl;
		}

		friend void exchange(Degree &,Fahrenheit &);
		friend void out(Degree &,Fahrenheit &);
};
// friend function definition without friend keyword.
void exchange(Degree &D,Fahrenheit &F) 
{
	if(D.d==0.0)
		D.d=(5.0/9.0)*(F.f-32);
	else
		F.f=(9*D.d/5.0)+32;
}

void out(Degree &D,Fahrenheit &F)
{
	cout<<"Degree ==> Fahrenheit "<<endl;
	cout<<"Fahrenheit value is "<<F.f<<endl;
	cout<<"Fahrenheit ==> Degree "<<endl;
	cout<<"Degree value is "<<D.d<<endl;
}

void main()
{
	clrscr();
	Degree D1,D2(-40);
	
        Fahrenheit F1,F2(50);
	exchange(D2,F1);
	exchange(D1,F2);
	
        out(D1,F1);
	getch();
}