It is undoubtedly a great advantage of object-oriented programming to encapsulate data with functions of processing data, form classes and realize data hiding. But sometimes the encapsulation is not absolute.
A friend function provides a mechanism for data sharing between member functions of different classes or objects, member functions of classes, and general functions. In layman's parlance, a friend relationship is a class that proactively declares which classes or functions are its friends and gives them access to this class. In other words, a normal function or a member function of a class can access data encapsulated in another class through a friend relationship.
To a certain extent, the friend is the destruction of data hiding and encapsulation, but for data sharing, improve the efficiency and readability of the program, in many cases this small damage is necessary.
In a class, use the keyword friend to declare other functions or classes as friends. If a friend is a member function of a generic function or class, it is called a friend function. If a friend is a class, it is called a friend class. All member functions of a friend class are automatically called friend functions.
friend function
A friend function is a non-member function that is decorated with a keyword friend in a class, either as a normal function or as a member function of another class.
Although it is not a member function of this class, it can access the class's private members and protected members through the object name in its function body.
For example: Use the friend function to calculate the distance between two points,
#include <iostream>
#include <cmath>
using namespace std;
Use the friend function to calculate the distance between two points
class point{public
: Point
(int xx = 0, int yy = 0) {X = XX; Y = yy;}
int GetX () {return X;}
int GetY () {return Y;}
Friend Float fdist (Point &a, point &b);
Private:
int X, Y;
};
Float fdist (Point &p1, point &p2) {
Double x = Double (P1. X-p2. X);//access private data members through objects instead of having to use the Getx () function
double y = double (p1. Y-p2. Y);
return float (sqrt (x*x + y*y));
}
int main () {point
P1 (1, 1), P2 (4, 5);
cout << "The distance is:";
cout << fdist (p1, p2) << endl;//calculates the distance between two points return
0;
}
You can see that the private data members X and y of the point class are accessed directly through the object name in the friend function Fdist ().
Friend class
Like a friend function, a class can declare another class as a friend class. If Class A is a friend function of Class B, all member functions in Class A are all friend functions of Class B and can access the private and protected members of Class B. For example:
Class a{public
:
int GetX () {return x;}
The Friend class b;//b class is a friend class/other member of Class A,
slightly
private:
int x;
};
Class b{public
:
void set (int i);
Other members slightly
private:
A;
};
void B:: set (int i) {
a.x = i;//because Class B is a friend class of Class A, you can access private members of Class A objects in member functions of B
About friends, the point to note:
Friend relationship cannot be passed
The friend relationship is one-way.
A cloud relationship cannot be inherited