1.
bool Equal (DayOfYear date1,dayofyear date2)
{return
(date1.month==date2.month&&date1.day== date2.day);
}
There is only one problem with this definition: it is illegal. This is illegal because the member variable month and day are private members of the DayOfYear. Private member variables (and private member functions) are usually not referenced in the body of a function, unless the function is a member function.
A friend function of a class is not a member function of this class, but rather a "friendly" function that accesses the private members of that class like a member function. A friend function can directly read the value of a member variable, or even directly change the value of a member variable.
When a declaration of a function is listed in the definition of a class, the. This function becomes a friend of the class, and you simply add a keyword friend before the function declaration. A friend is not a member function, it is essentially a normal function, but is specifically granted access to the data members of the class.
2. A simple rule can help you select member functions and non-member functions
1. If the function is to perform a task that involves an object, the member function is used.
2. If you want to perform a task involving multiple objects, use a non-member function.
3.const parameter modifier
Const applies to the calling object:
In the case of a member function, the keyword const is placed after the function declaration, just before the semicolon at the end, as follows:
Classs Money
{public
:
...
void output (ostream &outs) const;
Const is used in function declarations and function definitions, so the output function definition begins as follows:
void Money::output (Ostream & const)
{
...
}
4. Overloaded operator
Rules:
1. When overloading an operator, at least one argument must be a class type.
2, the overloaded operator can be a friend of the class; an operator function can be a member of a class, or it can be a normal function.
3, you cannot create a new operator. You can only overload existing operators, such as + 、-、 *,/,%, and so on.
4, can not change the priority of an operator.
5, the following operators can not be overloaded: dot operator, scope resolution operator:: and Operators. * and?: