The needs of friend functions and friend classes:
Class has an attribute of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as public, this also destroys the hidden attributes. In addition, you should see that in some cases, especially when multiple calls are made to some member functions, the time overhead is required because of parameter passing, type checking, and security checking, which affects the efficiency of the program.
In order to solve the above problems, this paper puts forward a scheme to make ufida. A friend is a generic function that is defined outside of a class, but it needs to be described in the class body, in order to differentiate it from the member functions of the class, and the keyword friend is preceded by the description. A friend is not a member function, but it can access private members in a class. The role of the friend is to improve the efficiency of the program, but it destroys the encapsulation and concealment of the class, making it possible for non-members to access the private members of the class.
A friend can be a function called a friend function; A friend can also be a class, which is called a friend class.
Friend function
A friend function is characterized by the ability to access a member function of a private member in a class. A friend function is syntactically the same as a normal function, that is, it is defined and called as a normal function. The following example illustrates the application of a friend function.
Copy Code code as follows:
#include "iostream"
#include "Cmath"
using namespace Std;
Class Point
{
Private
Double x,y;
Public
Point (double xx, double yy) {x=xx; y=yy;}
void Getxy ();
Friend Double Distance (Point &a, point &b); Friend identifies it as a friend function, not as a member function,
};
void Point::getxy ()
{
cout<< "(" <<x<< "," <<y<< ")" <<endl;
}
Double Distance (Point &a, point &b)
{
Double dx = a.x-b.x; can access private members in a class
Double dy = a.y-b.y;
return sqrt (dx*dx+dy*dy);
}
int main (void)
{
Point P1 (3.0, 4.0), P2 (6.0, 8.0);
P1. Getxy (); Calling member functions
P2. Getxy ();
Double d = Distance (P1, p2); When the function is called, it is also the same as the normal function call, do not call as a member function
cout<< "The distance is" <<d<<endl;
System ("pause");
return 0;
}
Description:The point class in the program illustrates a friend function distance (), which adds the friend keyword in front of the description, identifying it as not a member function, but a friend function. It is defined in the same way as a normal function definition, and differs from the definition of a member function because it does not need to indicate the class to which it belongs. However, it can refer to private members in a class, and a.x,b.x,a.y,b.y in the function body are private members of the class, which are referenced through objects. When you call the function, it is also the same as the normal function, do not call as a member function. In this case, p1. Getxy () and P2. Getxy () This is a call to a member function, to be represented by an object. and distance (P1, p2) is called the friend function, it is called directly, does not need the object representation, its argument is an object.
Friend class
Friends In addition to the previous mentioned functions, the friend can also be a class, that is, a class can be a friend of another class. When a class is a friend of another class, this means that all the member functions of the class are friend functions of another class.
Note When using the friend class:
(1) The friend relationship cannot be inherited.
(2) The friend relation is one-way, does not have the exchange sex. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, to see if there is a corresponding declaration in the class.
(3) The friendship relationship is not transitive. If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding declaration in class
C declares that a is its friend class, so the most basic is that a can use the private method or object in C.
Visible, A is the base class for B, and C is the base class for D. ABCD has the following relationships:
1.B New method cannot access private members of C
2.B a method inherited from a can access private members of C
3.A can access only private members in D that are inherited from C, and new private members in D cannot be accessed!
summed up:
(1) A friend relationship cannot be inherited, but access rights do not change for existing methods.
(2) If the method that overwrites the base class, the access permission changes
(3) Friendship relations are not transitive
If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A