Friend Function in C++
What is Friend Function in C++?
The Friend Function in C++ is referred to as a function that is defined outside of the class in C++ but it has access to the protected and private members of the class. There are some cases where there is a need for multiple classes to share their members. It can be class functions, data members, or function templates. In such situations, we add the desired function as a buddy to both classes, enabling access to members of the class’s private and protected data.
The friend function is defined globally outside the class scope. To define the friend function, the keyword friend is used by which the compiler understands the given function is the friend function. As they are defined as friend functions, they are the non-member functions that have access to and control over the private and protected members of the class.
It can be:
- global Function
- Member function of another class
Syntax of Friend Function in C++
1. For the global function
friend ret_type func_name (parameters);
Where,
friend is a keyword to define the friend function
ret_type is the return type of the friend function
func_name is the name of the friend function
parameters are the arguments of the friend function
2. For another class member function
friend ret_type class_name::func_name (parameters);
Where,
friend is a keyword to define the friend function
ret_type is the return type of the friend function
class_name is the name of the class in which the friend function is defined
func_name is the name of the friend function
parameters are the arguments of the friend function
How to declare a friend function in C++?
class class_name
{
friend ret_type func_name (parameters);
};
A class is declared in this, and a friend function is called with its return type and parameters.
Features of Friend Function in C++
- The friend function is not in the scope of the class in which it is declared as a friend.
- We cannot call the friend function using the object of the class in which it is declared.
- It can be declared as a normal function without any object.
- Member names cannot be declared directly and has to use an object name and dot membership operator with the member name. (For example, M.n, M is the object of the friend function and n is the member of the class.
- The friend function has objects as its parameters.
- We can declare friend function in the private or public method of class.
The concept of friend functions in C++ can be explained by its implementation in various cases.
Program 1: Program to understand the use of the friend function in C++.
CODE
#include <iostream>
#include<cmath>
using namespace std;
class Cube
{
private:
int value;
public:
Cube (): value (5) {}
friend int printCube (Cube); //friend function
};
int printCube (Cube c)
{
c.value=pow(c.value,3);
return c.value;
}
int main ()
{
Cube c;
cout <<" Cube of the value :" <<printCube (c) <<endl;
return 0;
}
OUTPUT
![Friend Function in C++](https://static.tutorialandexample.com/cpp/friend-function-in-cpp1.png)
In this code, a class Cube is defined to find the cube of the value. We have made a friend function printCube which has an object C of class Cube as a parameter. In the friend function definition, it has access to the private object of the class. We have declared the class object to find the cube of the value. And then, in the main function, the friend function is called and printed the result.
Program 2: Program to illustrate the functioning of the global function as Friend Function in C++.
CODE
#include<iostream>
using namespace std;
class Name
{
int a;
int b;
int c;
public:
void set_data (int m, int n, int p);
void disp(void);
friend void operato (Name &s);
};
void Name::set_data (int m, int n, int p)
{
a=m; b=n; c=p;
}
void Name::disp(void)
{
cout<<a<<" "<<b<<" "<<c<<"\n";
}
void operato (Name &s)
{
s.a = -s.a;
s.b = -s.b;
s.c = -s.c;
}
int main ()
{
Name s;
s. set_data (2,6,8);
cout<<"S:";
s. disp ();
operato(s);
cout<<"-S:";
s. disp ();
return 0;
}
OUTPUT
![Friend Function in C++](https://static.tutorialandexample.com/cpp/friend-function-in-cpp2.png)
In this program, we have made a class Name in which a friend function operato is declared which can access the private variables of the class in its definition. Global functions are made which get access to the members of the class. The friend function operato is made which prints the negative of the value.
Let’s understand the concept of global function as a friend function with another program in C++.
CODE
#include <iostream>
using namespace std;
class Main {
private:
int private_var;
protected:
int protected_var;
public:
Main ()
{
private_var = 60;
protected_var = 100;
}
friend void friendF (Main& m);
};
void friendF (Main& m)
{
cout << "Value of Private Variable: " << m.private_var << endl;
cout << "Value of Protected Variable: " << m.protected_var;
}
int main()
{
Main obj;
friendF (obj);
return 0;
}
OUTPUT
![Friend Function in C++](https://static.tutorialandexample.com/cpp/friend-function-in-cpp3.png)
In this program, we have made the global function friendF a friend function that prints the protected and private variable values.
Program 3: Program to demonstrate the use of the friend function in two classes.
CODE
#include<iostream>
using namespace std;
class SECOND; //forward declaration.
class FIRST
{
int f;
public:
void data (int i)
{
f=i;
}
friend void maximum (FIRST, SECOND); //friend function.
} ;
class SECOND
{
int s;
public:
void data (int i)
{
s=i;
}
friend void maximum (FIRST, SECOND);
};
void maximum (FIRST f1, SECOND s1)
{
if (f1.f >= s1.s)
std:: cout<< "The greater Value is: " << f1.f << std::endl;
else
std::cout<< "The greater value is: " << s1.s << std::endl;
}
int main ()
{
FIRST f1;
SECOND s1;
f1. data (100);
s1. data (200);
maximum (f1, s1);
return 0;
}
OUTPUT
![Friend Function in C++](https://static.tutorialandexample.com/cpp/friend-function-in-cpp4.png)
In this program, we have declared two classes FIRST and SECOND, and in both classes we have used the same friend function maximum. It accessed the private variables of both classes.
Uses of Friend Functions in C++
- Friend Functions are used to access the private or protected members of the class.
- They are also used in operator overloading.
- The member functions of one class can be declared as the friend function of others.
- In cases where two classes need a single function to access their members, we use the friend function for these conditions.