C + + reading notes--Definition of class

Source: Internet
Author: User

(a word hit the keyboard a word!!!) )

1. Syntax for class definitions

The class definition starts with the keyword class and defines the following syntax:

Class Name {

Access Rights control:

Declaration of a class member

the up part is the class definition body (body), which mainly includes the access descriptor and the declaration of the class member, the common access description have public, private, and proteced, which restrict access to the members.

2. Access control permissions

The C + + language supports class access control permissions with public, protected, and private 3 levels, which are used by the 3 member access specifiers to control access to class members.

The public permission is the highest permission, and a member with that permission can be accessed outside the scope of the class.

Private permissions are the minimum access rights, and members with that permission can only be accessed in the class scope.

Protected permissions are roughly between public and private.

!! The only difference between a struct and a class is that the default access rights are different: the default access rights for struct members are public, and the default access rights for classes are private.

3. Data members and member functions

Classes can encapsulate the properties of things and their behavior as a whole, whose main attributes are abstracted as critical data and represented as data members of a class. Its typical behavior is abstracted as a key operation and represented as a member function of a class.

Because the data members of a class are generally the key data of a class, they are not allowed to be accessed and modified at will, so they need to be granted private access, and the member functions of the class are to provide a set of services or interfaces, generally to set their access rights to public.

4. Scope operators

member functions can be defined in the body of a class definition, or outside the class definition body. When member functions are simpler and implementation code is small, they can be defined in the class definition body, while complex, code-rich member functions are generally defined outside the class definition body. When you define a member function outside the body of a class definition, you need to indicate the space that it belongs to (that is, the generic relationship), or the function is considered to be a global function (which has nothing to do with the class) and generates an access error. The generic relationship that describes the member function requires the scope operator "::".

5. Properties of member functions

A member function is a member of a class that, when defined, requires an explicit description of its generic relationship, which is typically called by the object and DOT operators when called. As a function, member functions also have the properties of ordinary functions.

(1) Connectivity: Member functions defined in the body of a class definition are generally treated as inline functions, and member functions defined outside the class definition body have external connectivity.

(2) Overloading: As with normal functions, member functions can also be overloaded.

(3) Default parameter values: Like normal functions, member functions can have default parameter values.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + reading notes--Definition of class

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.