Youyuan can be a function calledYouyuan FunctionYouyuan can also be a class calledYouyuan class.
Class has the characteristics of encapsulation and information hiding. Only member functions of the class can access private members of the class. Other functions in the program cannot access private members. A non-member function can be a public member in the category. However, if all data members are defined as public members, this destroys the hidden features.
In addition, it should be seen that in some cases, especially when calling some member functions for multiple times, the time overhead is required due to parameter transmission, type check, and security check, this affects the program running efficiency.
In order to solve the above problems, we propose a solution to use youyuan. Youyuan is a common function defined outside the class, but it needs to be described in the class body. In order to distinguish it from the member functions of the class, the keyword friend is used before the description. Youyuan is not a member function, but it can be a private member in the member class. Youyuan is used to improve the program running efficiency. However, it destroys the encapsulation and hiding of classes and enables non-member functions to be private members of the category.
Youyuan Function
A friend function is a non-member function that can be a private member in the member class. In terms of syntax, youyuan functions are the same as normal functions, that is, they are defined and called in the same way as normal functions. The following example shows the application of the functions.
- #include
- #include
- class Point
- {
- public:
- Point(double xx, double yy) { x=xx; y=yy; }
- void Getxy();
- friend double Distance(Point &a, Point &b);
- private:
- double x, y;
- };
- void Point::Getxy()
- {
- cout<<"("<<<","<<Y<<")"<<ENDL;< FONT>
- }
- double Distance(Point &a, Point &b)
- {
- double dx = a.x - b.x;
- double dy = a.y - b.y;
- return sqrt(dx*dx+dy*dy);
- }
- void main()
- {
- Point p1(3.0, 4.0), p2(6.0, 8.0);
- p1.Getxy();
- p2.Getxy();
- double d = Distance(p1, p2);
- cout<<"Distance is"<<
FONT>
}
Note: The Point class in this program describes a friend's meta function Distance (). It adds the friend keyword before the description to identify that it is not a member function, but a friend's meta function. Its definition method is the same as that of a common function, but different from that of a member function, because it does not need to specify the class to which it belongs. However, it can reference. x, B. x,. y, B. y is a private member of a class, which is referenced by an object. When calling a friend function, it is also the same as calling a common function. Do not call it like a member function.
In this example, p1.Getxy () and p2.Getxy () are member function calls and are represented by objects. Distance (p1, p2) is the call of a friend function. It is called directly without object representation. Its parameter is an object.
Youyuan class
In addition to the previously mentioned functions, youyuan can also be a class, that is, a class can be used as a friend of another class. When a class acts as a friend of another class, this means that all member functions of this class are friends of another class.
Note:
(1) Friendship cannot be inherited.
(2) The relationship between friends and friends is unidirectional and not interchangeable. If Class B is A friend of Class A, Class A is not necessarily A friend of class B. It depends on whether there is A corresponding declaration in the class.
(3) The relationship between friends and friends is not transmitted. If Class B is A friend of Class A, Class C is A friend of Class B, and class C is not necessarily A friend of Class A, it also depends on whether there is A corresponding statement in the class
To sum up:
(1) the relationship between friends and friends cannot be inherited, but the access permissions of existing methods remain unchanged.
(2) If the method of the base class is modified, the access permission is changed.
(3) The relationship between friends and friends is not passed.
If Class B is A friend of Class A, Class C is B's friend, and class C is not necessarily A friend of Class.
We hope that the above content will help you.