Friend Function, Friend class

Sep 4, 2018

The OOP feature of c++, the data hiding and encapsulation introduced do not allow the object's private data to access from non-member functions and from outside the class. To use and manipulate the objects private data we must use public member functions. But, this type of mechanism is not always convenient. for example, if we want to perform certain operations between two objects then there will arise some sort of diffuculty; the scenario for this case is to use the private data members of the object from functions that are outside the class. Such mechanism is achieved in c++ with friend function. The friend function makes easier to solve the problem of the situation above, and the private data members are accessible within those functions only to which the class says friend.

To declare friend function of a class keyword friend is prefixed before function header. The friend function declaration can be placed in either private, protected or public part of the class, and the meaning is same. The function declared as a friend of a class can be global function or function in any scope.

syntax:

class class_name{

    private:

        //................

    public:

        //................

        friend return_type function_name(arg1, arg2, ......argn);

};

regarding friend function, the following points are important.

  • It is not in the scope of the class within which it has been declared as friend.
  • Friend function is not the member of the class, and it can not be invoked using the object of the class; they can be called like normal c++ functions.
  • To access private data members within the friend function, . operator is used.
  • Wherever it is declared (private, public, protected) the meaning is same.

 

Let's consider an example to illustrate the friend function, the program below adds two complex numbers using friend function concept.

#include
using namespace std;

class Complex{
	private:
		float real;
		float imag;
	public:
		// default constructor
		Complex(){
			real = 0;
			imag = 0;
		}
		//parameterized constructor
		Complex(float r, float i){
			real = r;
			imag = i;
		}
		// friend function declaration for addition
		friend Complex add(Complex c1, Complex c2);
		// function to show the values
		void display(){
			cout<<"("<<real<<","<<imag<<")"<<endl;
		}
};
// friend function defination
Complex add(Complex c1, Complex c2){
	Complex temp;
	temp.real = c1.real + c2.real;
	temp.imag = c1.imag + c2.imag;
	return temp;
}

int main(){
	Complex cc1(4.6, 5.6), cc2(4.3, 4.7),c3;
	cc1.display();
	cc2.display();
	c3 = add(cc1,cc2);
	cout<<"The sum of the complex numbers:"<<endl;
	c3.display();
	return 0;
}

 

Sample Run:

(4.6,5.6)
(4.3,4.7)
The sum of the complex numbers:
(8.9,10.3)

 

 

Friend class:

Any function can be friend of one class, function that are member of another class can also be friend of one class. If all the members of the class are to be declared as friend functions the it requires multiple friend declarations. The shorcut process for this is friend class. The syntax is:

class class_nameA;

class class_nameB{

    //statements......

    friend class class_nameA;

};

The example below illustrates the concept.

#include
using namespace std;

class A;
class B{
	private:
		int data;
	public:
		// here the constructor works as both
		//default constructor and parameterized constructor
		B(int d = 0){
			data = d;
		}
		friend class A;
};

class A{
	public:
		void showPropertyOfB(B b){
			cout<<"Private Property of B from inside A :"<<b.data<<endl;
		}
};

int main(){
	B myB(400);
	A myA;
	myA.showPropertyOfB(myB);
	return 0;
}

 

Sample Run:

Private Property of B from inside A :400

 

Related


Tools/Apps





© Nepal Exchange Rates