C ++ Reading Notes-class definition
(One word and one word on the keyboard !!!)
1. Class Definition syntax
The class definition starts with the keyword class. The syntax is as follows:
Class name {
Access permission controller:
// Class member declaration
}; The part above is the class definition body, which mainly includes the access permission specifiers and class member declarations. Common access permission specifiers include public, private, and proteced, they restrict access permissions to members.
2. Access Control Permissions
The c ++ language supports three levels of class access control permissions: public, protected, and private. These three member access specifiers are used to control access to class members.
The public permission is the highest permission. Members with this permission can be accessed outside the class scope.
The private permission is the minimum access permission. Members with this permission can only be accessed in the class scope.
The protected permission is roughly between public and private.
!! The only difference between the structure and the class is that the default access permissions of the two are different: the default access permissions of the structure members are public, and the default access permissions of the class are private.
3. Data members and member functions
Class can encapsulate the attributes and behaviors of a transaction into a whole. Its main attributes are abstracted as key data and expressed as data members of the class. Typical behaviors are abstracted as key operations and represented as member functions of the class.
The data members of a class are generally key data of the class and cannot be accessed and modified at will. Therefore, you need to set their access permissions to private, the member functions of a class are to provide a set of services or interfaces. Generally, they must set their access permissions to public.
4. Scope Operators
Member functions can be defined in the class definition body or in the class definition body. When the member functions are relatively simple and the implementation code is small, they can be defined in the class definition body, while the complex member functions with more code are generally defined in the class definition body. When defining a member function in a class in vitro, you must specify the space to which the function belongs (that is, the class relation). Otherwise, the function is considered a global function (which has no relationship with the class) an access error occurs. The scope operator ":" is required for the class relationship of member functions.
5. Nature of member functions
A member function is a member of a class. It must explicitly describe its class relationship during definition. It is generally called by the object and the dot operator. As a function, member functions also have the nature of common functions.
(1) Connectivity: member functions defined in the class definition body are generally processed as inline functions. member functions defined in the class definition body have external connectivity.
(2) overload: Like normal functions, member functions can also be overloaded.
(3) default parameter values: like common functions, member functions can also have default parameter values.