C ++ Class Definition Format description in C ++

Source: Internet
Author: User

C ++ class definition is a bit like the structure in C language. The only difference is that the structure does not define "data-related operations". Therefore, classes in C ++ are very abstract, the data in the class is hidden, and the class is encapsulated.

The structure defined by C ++ classes (that is, the composition of classes) is used to determine the behavior of a class of objects, these behaviors are determined by the internal data structure of the class and related operations. These actions are described through an operation interface (that is, the member functions of the classes we usually see ).

Users only care about the functions of interfaces (that is, the functions of member functions of classes) and are not interested in how they are implemented. The operation interface is also called the service that this type of object provides to other objects.

C ++ Class Definition Format

The Definition Format of a class is generally divided into the description and implementation sections. The description section describes the members in the class, including descriptions of data members and member functions. A member function is used to operate data members. It is also called a "method ". The implementation part is used to define member functions. In summary, the description part tells the user what to do, and the Implementation part tells the user how to do it ".

The General Definition Format of the class is as follows:

 
 
  1. Class<Class Name> 
  2. {
  3. Public:
  4. <Description of member functions or data members> 
  5. Private:
  6. <Description of data members or member functions> 
  7. };
  8. <Implementation of member functions> 

The following is a simple description of the above format: class is the key word of the defined class. <class Name> is a kind of identifier. Generally, a string starting with a letter T is used as the class name. A pair of braces contains the class description section (including the previous Class header) indicating the class members.

The class member includes two parts: data member and member function. In terms of access permissions, class members are classified into public, private, and protected. Public members use public to describe that the public part is usually an operation (that is, a member function), which is an interface function provided to the user. These members can be referenced in the program.

Private Members use private to describe the attributes of objects in the class. Users cannot access them, only member functions or specially described functions can reference them. They are hidden parts. The protection class (protected) will be introduced later.

The keywords public, private, and protected are called access permission modifiers or access control modifiers. They appear in the class body (that is, within a pair of curly brackets) in order of irrelevant, and allowed to appear multiple times, they are used to describe the access permissions of class members.

<Implementation of each member function> is the implementation part of the class definition, which includes the definition of all functions described in the class body. If the class body of a member function is defined, the implementation part will not appear. If all member functions are defined in the C ++ class, the implementation part can be omitted.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.