1. youyuan Function
(1) A friend function is a common function defined outside the class.
A friend function is the same as a normal function. In a class, the normal function must be declared as a friend.
(2) user functions are not member functions.
You can call a function directly instead of using an object. You can use the public, protected, and private member of the feature class, but you must use an object, Object Pointer, or object reference to access the function.
2. User meta function declaration
Function name of the friend return value type (parameter table );
In the class, you only need to place this declaration in the public part.
Class Point
{
Double x, y;
Public:
Point () {x = 0.0; y = 0.0 ;}
Point (double xx, double yy) {x = xx; y = yy ;}
Friend double distance (Point, Point); // declare the distance function as a friend Function
};
Double distance (Point my1, Point my2)
{
Return sqrt (my1.x-my2.x) * (my1.y-my2.y ));
}
3. When a friend functions are defined, they are the definitions of common functions. friend is not added before and must be added when the class is declared.
You can access private, public, and protected members within the scope of the objects that enable the functions.
The main function cannot be declared as a class membership function. The main function can only be called as the main function, but cannot be called.
1. Copy the constructor
(1) A copy constructor is a constructor that belongs to a class member function (generally defined as public). It has the same name as the class name but has no return value. When an object is created, if the initialization value of this object is another similar object (Value assignment is not called), it is called.
(2) copy the declaration of the constructor
Class Name: Class Name (Class Name & Object Reference name); or another declaration method
Class Name: Class Name (const Class Name & Object Reference name)
Note: The copy constructor has only one parameter, and the parameter must be referenced by an object. Each class must have a copy constructor. If no explicit definition is provided, the system automatically defines and sets its attribute to public.
2. Example
Class Point
{
Int x, y;
Public:
Point () {x = 0; y = 0 ;}
Point (int xx, int yy) {x = xx; y = yy ;}
Point (Point & pf) {x = pf. x; y = pf. y;} // The statement for copying constructors can omit the First Class Name and ::
};
Point (Point & pf)
{
* This = pf; // completes the copy construction.
}
Int main ()
{
Point p1; // call construction without Parameters
Point p2 (3, 4); // call Construction
Point p3 (p2); // call the copy Structure
}
Note: If the const modifier is not used, the statement pf. x = 8 is legal, but it is invalid after the const is added. In addition, pf will be released after being referenced.