Properties of Friend Function in C++
What is a friend class in C++?
When a class is declared as a buddy, it gains access to private and protected members of other classes. Permitting a certain class to access private and protected members of other classes can be helpful at times. For example, a LinkedList class might be permitted to access Node's secret members.
Syntax:
friend class class_name;
Example:
#include <iostream>
using namespace std;
class Example
{
private:
int var1;
protected:
int var2;
public:
Example()
{
var1 = 20;
var2 = 100;
}
friend class Friend;
};
class Friend
{
public:
void display(Example & t)
{
cout << "The value of Private Variable = "
<< t.var1 << endl;
cout << "The value of Protected Variable = "
<< t.var2;
}
};
int main()
{
Example e;
Friend fri;
fri.display(e);
return 0;
}
Output:
![Properties of Friend Function in C++/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/properties-of-friend-function-in-cpp1.png)
Friend Function:
In C++, a friend function can have exclusive access to secret and protected members of a class, just like a friend class. Since they have been declared friends, these non-member functions have access to and control over the class's private and protected members.
- Friends should only be utilized in certain situations. In object-oriented programming, the utility of encapsulating distinct classes is diminished when an excessive number of functions or external classes are declared as friends of a class with access to protected or private data.
- Mutual friendship does not exist. Even if class A is B's friend, this does not mean that B automatically becomes friends with A.
- It is not inherited to be friends. (For further information, see this.) In Java, the idea of friends does not exist.
A friend function can be:
- A global function
- A member function of another class
1. A global function:
Syntax:
Friend functions can be defined for any global function.
friend return_type function_name (args);
Example:
#include <iostream>
using namespace std;
class example
{
private:
int var1;
protected:
int var2;
public:
example()
{
var1 = 20;
var2 = 100;
}
friend void friendFunction(example& obj);
};
void friendFunction(example& obj)
{
cout << "the value of Private Variable: " << obj.var1
<< endl;
cout << "the value of Protected Variable: " << obj.var2;
}
int main()
{
example object1;
friendFunction(object1);
return 0;
}
Output:
![Properties of Friend Function in C++/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/properties-of-friend-function-in-cpp2.png)
2. A member function of another class:
In C++, a member function of a different class can also be declared as a buddy function.
Syntax:
friend return_type class_name::function_name (arguments);
Example:
#include <iostream>
using namespace std;
class base;
class anotherClass {
public:
void memberFunction(base& obj);
};
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 20;
protected_variable = 100;
}
friend void anotherClass::memberFunction(base&);
};
void anotherClass::memberFunction(base& obj)
{
cout << "the value of Private Variable: " << obj.private_variable
<< endl;
cout << "the value of Protected Variable: " << obj.protected_variable;
}
int main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);
return 0;
}
Output:
![Properties of Friend Function in C++/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/properties-of-friend-function-in-cpp3.png)
Benefits of Friend Functions:
- A friend function can access members without requiring it to inherit the class.
- By gaining access to two classes' confidential data, the friend function serves as a mediator between them.
- It can be applied to make overworked operators more versatile.
- It may be announced in the class's protected, private, or public areas.
The Drawbacks of Friend Functions:
- The law of data concealing is broken by friend functions, which provide access to private members of a class from outside the class.
- Friend functions are not allowed to do any run-time member polymorphism.
Features of Friend function:
- In C++, a buddy function is a unique function that has access to a class's private and protected data even though it isn't a member function of the class.
- A friend function is an ordinary or non-member function of a class that is designated as such within the class by using the term "friend." By defining itself as a friend, a function can grant all access permissions.
- The friend function's declaration function is the only place the keyword "friend" appears; neither the function definition nor the function call contains it.
- Like an ordinary function, a friend function is called. The dot operator and the object name cannot be used to invoke it. It could take the object as an input whose value it wishes to retrieve.
- Any class section, whether public, private, or protected, may declare a friend function.