Characteristics of friend function in C++
Introduction
Unlike other types of functions in C++, friend functions have access to a class's private as well as protected data members despite not being a member of the class itself. that a result, a companion method is given the same control over the classes' internals that a function that is a member would. Friend functions, which are used to grant restricted external access to class characteristics, are often defined inside the object with which they are intended to be companions. Although they provide a means of working with people who are private, their use should be prudent and restricted to circumstances.
Friend function usage in C++
As was previously said, anytime we need to get to the data of a class's secret or secure participants, friend functions are necessary. This only applies if we don't wish to have access to these secret or secured members using the components of that class. Let's compare the two categories London and Brazil for a better understanding this. We could need the metropolitan () function to have unrestricted access to each of these two classes. We will need an instance of these classes in order to obtain all the members without the friend function. In C++, friend functions enable us to stay out of situations where a function's access requires membership in one of these types.
Scope of a friend function in C++
In contrast to conventional functions, friend functions can be declared beyond the category in which they are stated. It is not required to be applied on an instance of a class.
Declaration of friend class
class
{
friend (argument/stype);
};
Features of Friend Functions
- Accessibility to secret and guarded members of a class is made possible via friend functions used in C++ when outside the class
- They enable explicit access to the class internals using the friend function. Friend functions controllably reveal a portion of a class's capabilities to outside code.
- It gives other program components only limited access to internal information that we deem appropriate to reveal. This allows for flexibility and reuse while concealing implementation specifics that should be abstracted.
- In C++, the buddy function can be utilized to call (invoke) every instance of any kind of class (object) similar to any other function.
- In C++, a friend function is unable to access a class's protected or private information elements. For access to the information stored in members, you must use an object (an instance of that class), followed by the dot operator (.)
- The friend term is used for declaring friend functions within a class in the programming language C++ By doing so, they are designated as "friends" of the class and are given access without the usual limitations.They have unrestricted access to guarded and exclusive members just like the general public.
Friend Functions Implementation
There are two ways to carry out friend functions:
- An approach from a different class
- When we need to access a class's confidential individuals, we define a friend class.
A global function:
The global class declaration's private and protected members can all be accessed via a "global friend function."
A straightforward C++ buddy function example that print the area
Code
#include <iostream>
#include <string>
using namespace std;
class Area{
int length,breadth,area;
public:
Area(int length,int breadth):length(length),breadth(breadth) {}
void calcArea(){
area = length * breadth;
}
friend class printClass;
};
class printClass{
public:
void printArea(Area d){
cout<<"Area = "<<d.area;
}
};
int main(){
Area d(11,17);
d.calcArea();
printClass p;
p.printArea(d);
return 0;
}
![Characteristics of friend function in C++](https://static.tutorialandexample.com/cpp/characteristics-of-friend-function-in-cpp1.png)
Benefits Of The C++ Friend Function
- Developers can create code that is more effective thanks to the friend function.
- It enables the sharing of private class data with a non-member function.
- It rapidly acquires entry to the private members of a class.
- It is frequently utilized when there are students in multiple classes who have connections to other parts of the program.
- It makes possible additional capabilities that the class does not often use.
- Overworked operators' flexibility is improved.
Limitations:
- Whenever flexible binding is required, buddy functions need an additional line of code, which is their biggest drawback.
- A private (usually guarded) digital member function should be activated by the friend function in order to mimic the effects of a digital friend. The Online Friend Function Phrase is what is used to describe this.
Conclusion
While ensuring encapsulation and protocol consistency, friend functions and friend classes enable adaptable and chosen access to class members. They stand for a potent C++ functionality that you ought to apply to your code.