What is Friend Function?
Friend function is a non-member function that can access the private and protected members of a class. “Friend” is a keyword used to indicate that a function is the friend of a class. This allows the compiler to know that the particular function is a friend of the given class. The friend function then should be able to access the private and protected member of a given class. Without the friend keyword, a non-member outside function can only access the public members of a class.
Key Features of Friend Function:
Here are the key features of the friend function:
- A friend function is not in the scope of the class.
- The friend function cannot be invoked using an instance of a class.
- It can access the members using the object and dot operator.
Syntax of Friend Function:
Here is the syntax of the friend function:
{
………………………………………
………………………………………
friend return_Type function_Name(arg_1, arg_2, …);
};
Example of Friend Function:
Now, let us look into an example program to understand the concept of the friend function in C++. In the below example program, we have the “Friend_Demo” class. It has three different types of data members, i.e., private, protected, and public.
We have defined another function, i.e., “friendDemo_Func()” outside the scope of the “Friend_Demo” class and tried to access the members (private, protected, and public) of the “Friend_Demo” class.
But, as you can see in the output below when we compile the program, it throws compilation errors. The friend function is going to exactly solve this problem.
using namespace std;
class Friend_Demo
{
private:
int i_private;
protected:
int i_protected;
public:
int i_public;
};
void friendDemo_Func()
{
Friend_Demo fd;
fd.i_private = 10;
fd.i_protected = 15;
fd.i_public = 20;
cout << fd.i_private << endl;
cout << fd.i_protected << endl;
cout << fd.i_public << endl;
}
int main()
{
friendDemo_Func();
return 0;
}
In the previous program, we were getting compilation errors while trying to access the private, protected, and public members of a class from a non-member function. This is because a non-member function is not allowed to access the private and protected members of a class from outside the scope of the class.
Now, in this example, we have declared “friendDemo_Func()” function as a friend inside the scope of the class, i.e., “Friend_Demo”:
We have created an object, i.e., “fd” of the “Friend_Demo” class inside the “friendDemo_Func()” function. Now, we can access the private, protected, and public members of the “Friend_Demo” class using the dot operator. We have assigned 10, 15, and 20 to i_private, i_protected, and i_public, respectively.
As you can see in the output below, this program is now compiled and executed without any errors and print the output as expected.
using namespace std;
class Friend_Demo
{
private:
int i_private;
protected:
int i_protected;
public:
int i_public;
friend void friendDemo_Func();
};
void friendDemo_Func()
{
Friend_Demo fd;
fd.i_private = 10;
fd.i_protected = 15;
fd.i_public = 20;
cout << fd.i_private << endl;
cout << fd.i_protected << endl;
cout << fd.i_public << endl;
}
int main()
{
friendDemo_Func();
return 0;
}
Conclusion:
In this article, I have explained the concept of the friend function in C++. I have also shown two working examples to explain how the friend function behaves in C++. Sometimes, the friend function can be very useful in a complex programming environment. However, a programmer should be cautious about overusing it and compromising its OOP features.
from Linux Hint https://ift.tt/300E7Go
0 Comments