How to correctly define C ++ classes

Source: Internet
Author: User

The following is a detailed analysis of the C ++ class. C ++ class is an abstraction of a class of objects, and objects are instances of a class. Therefore, classes and objects are closely related. Because classes and objects are highly correlated, you must pay attention to them when performing C ++ class operations.

What is a C ++ class?

C ++ is a complex data type, which encapsulates different types of data and operations related to the data. This is a bit like the structure in C. The only difference is that the structure does not define "data-related operations". "data-related operations" are the "methods" we usually see ", therefore, classes are more abstract, data in classes is hidden, and classes are encapsulated.

The class structure (that is, the composition of the class) is used to determine the behavior of a class of objects, and these behaviors are determined through 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.

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 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. From the perspective of access permissions, C ++ class members can be divided 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 C ++ class, only member functions or specially described functions can reference them. They are hidden parts. The protection class (protected) will be introduced later.

  • Illustration of Visual C ++ 5.0 interface differences
  • Introduction to Visual C ++
  • C ++ destructor and this pointer
  • Notes for first contact with C/C ++
  • C ++ Code Description

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 C ++ class. If the class body of a member function is defined, the implementation part will not appear. If all member functions are defined in the class body, the implementation part can be omitted.

The following is an example of defining a date class:

 
 
  1. Class TDate
  2. {
  3. Public:
  4. Void SetDate (int y, int m, int d );
  5. Int IsLeapYear ();
  6. Void Print ();
  7. Private:
  8. Int year, month, day;
  9. };
  10.  
  11. // Class implementation
  12. Void TDate: SetDate (int y, int m, int d)
  13. {
  14. YYear= Y;
  15. MMonth= M;
  16. DDay= D;
  17. }
  18.  
  19. Int TDate: IsLeapYear ()
  20. {
  21. Return (year %4= 0 & year % 100! = 0) | (year %400= 0 );
  22. }

Here, the scope OPERATOR: is used to identify the class of a member function, so that the implementation of the member function (that is, the definition of the function) is written in the class body, therefore, the class implementation is omitted. If a member function is defined in the external class, the class ID of the function must be added before the function header. The scope operator is used.

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.