After adding the keyword const to the form parameter table, you can declare the member function as a constant. The const Member cannot change the data member of the object to which it operates. The const must appear in the declaration and definition at the same time.
Two important advantages of data abstraction and encapsulation:
1) Avoid user-level errors that are unintentional inside the class and may damage the object status.
2) class implementation can be improved based on demand changes or defect reports over time without changing the user levelCode.
Constructor initialization type
Initialization list
Sales_item: sales_item (const string & book): ISBN (book), units_sold, revenue (0.0 ){}
Equivalent
Sales_item: sales_item (const string & book)
{
ISBN = book; units_sold = 0; revenue = 0.0;
}
What is the meaning of initializing data members and assigning values to data members? What is the difference?
First, the data members are classified by type and described as follows:
1. built-in data type, composite type (pointer, reference)
In the member initialization list and constructor body, the performance and results are the same.
2. User-Defined type (class type)
The results are the same, but the performance varies greatly. Because the data member objects of the class type have been constructed before they enter the function body, that is, the object construction work is performed at the member initialization list, the constructor is called, and after entering the function body, assign values to the constructed class objects and call a copy assignment operator (if not provided, use the default assignment by members provided by the compiler)
No member of the default constructor class type, and a member of the const or reference type must be in the constructor initialization list.
If you do not initialize a built-in or Composite Type constructor, it will put those members in an undefined state. Except as the target of the assignment, it is wrong to use an undefined member in any way.
When the constructor is declared as explicit, the compiler will not use it as the conversion operator. Unless you want to define implicit conversion for obvious reasons, the single-parameter constructor should be explicit. Setting the paparazzi function to explicit can avoid errors. You can explicitly construct objects when installing and using the function.
Youyuan: allows a class to grant access permissions to its non-public members to a specified function or class.
Youyuan can be a common non-member function, a member function of another class defined earlier, or a whole class. Set a class to youyuan. All member functions of the youyuan class can access non-public members of the class authorized to the youyuan relationship.
Advantages of static members
1) The name of the static member is in the scope of the class, so it can be avoided with other class members or Global Object Name objects.
2) encapsulation can be implemented. Static members can make private members. Global Objects cannot be private members.
3) readProgramIt is easy to see that static members are associated with a specific class, which clearly shows the programmer's intent.
Static members are components of a class rather than components of any object. Therefore, without this pointer, they cannot be declared as const or virtual functions.
When the class definition body references static members of a class, you must specify which class the Member is defined in.
The static keyword can only be used in the Declaration within the class definition.