C ++ Reading Notes-class definition

Source: Internet
Author: User

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.