Advantage And Disadvantage Friend Function C++

We have created the friend function to access the different modifiers in C++ Object Oriented Programming System such as Protected, Private and Public. The friend function or the friend class will usually be defined outside the course but still has access to the class's private, protected and public members defined in the class code.

Advantages Disadvantages
The declaration can be anywhere in the code It is not passed to the derived class
There is no need to create an object to call it. They don't have a storage-specified class.
Non-public members of the class can also be accessed using the friend function. The friend function comes in handy when multiple classes are tied together.
It can add extra functionality. Allows private and protected members to be shown as the information of the class.
Enables programming experience to be more efficient than ever before. It can have both public and private protected members in the same class in which it has been defined.

C++ code

Example

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
#include<iostream>
using namespace std;

// the below class declaration is a forward declaration
class JTP;
class XYZ
{
	int x;
	public:
		void set_data(int a)
		{
			x=a;
		}
		
		friend void max(XYZ, JTP);
};

class JTP {
	int y;
	public:
		void set_data(int a)
		{
			y=a;
		}	
		
	friend void max(XYZ, JTP);
};

void max(XYZ t1,JTP t2)
{
	if(t1.x>t2.y)
		cout<<t1.x;
	else
		cout<<t2.y;
}

// the driver code functionality starts from here
int main()
{
	JTP _abc;
	XYZ _xyz;
	_xyz.set_data(120);
	_abc.set_data(3135);
	
	// the below code snippet helps us with calling down the friend
	// function which we have declared to perform operations
	max(_xyz,_abc); 
	return 0;
}

Output:

Output

/tmp /mB gjSey FC Z.o
3135

C++ code

Example

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
#include<iostream>
using namespace std;

// defining class A from here
class A
{
	int x;
		public:
			
	A()
	{
		x=3979;
	}
	// the below small code snippet is enough to define the 
	// friend class for our code to demonstrate the usage of class B
	friend class B; 
};
// defining class B from here
class B
{
	public:
		void display(A &t)
		{
			cout<<endl<<"After performing the operations the value for X we have got as: "<<t.x;
		}
};

// the driver code functionality starts from here
main()
{
	A _a;
	B _b;
	_b.display(_a);
	return 0;
}

Output:

Output

/tmp /mB gjSey FC Z.o
After performing the operations, the value for X we have got is 3979

C++ code

Example

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
// and to find the largest of two numbers using the friend function method

#include<iostream>
using namespace std;

class Largest
{
    // declaring the variables a,b and m for performing the operations
	int a,b,m;
	public:
		void set_data();
		friend void find_max(Largest);	
};

// writing the void function to take the inputs from the user without 
// writing them again and again from the int main function
void Largest::set_data()
{
    // string display to ask the user to give the input
	cout<<"Dear User, please enter the value for the variable first no: ";
	cin>>a;
	cout<<"Dear User, please enter the value for the variable second no: ";
	cin>>b;
}

// function, which calculates the largest among the inputs entered by the 
// user
void find_max(Largest t)
{
	if(t.a>t.b)
		t.m=t.a;
	else
		t.m=t.b;
		
		Cout <<"Hey! User, the maximum number among the inputs entered by you is: "<<t.m;
}

// the driver code functionality starts from here
main()
{
	Largest l;
	l.set_data();
	find_max(l);
	return 0;
}

Output:

Output

/tmp /mB gjSey FC Z.o
Dear User, please enter the value for the variable first no: 35
Dear User, please enter the value for the variable second no: 365
Hey! User, the maximum number among the inputs entered by you is: 365

Input Required

This code uses input(). Please provide values below: