Class in C + + and spinach platform Dragon

Source: Internet
Author: User

For the spinach platform One-stop, need to search dsluntan.com object The most important feature is the concept of class and object. When developing an application in an object-oriented manner, the various things that are encountered are abstracted into classes, which typically contain the methods of data and manipulation of the data, and the user accesses the data and methods in the class by instantiating the class object.

Definition of the class:

Class/struct Class name//class header

{Definition of data and methods}; Class Body

1. The data in the class body describes the characteristics (attributes) of the class, which is the function defined in the class that describes the behavior of the class.

2. The only difference between using class and struct is that classes defined with class are not forced to be qualified, their members are private and cannot be called externally. If you use a struct, its members are public and can be called externally.

3. The class body is surrounded by curly braces to form a new scope. The name defined within the class must be unique, but it can be duplicated with an externally defined name.

4, c++11 the new standard stipulates that data members can be provided with a class initial value. When an object is created, the initial value in the class is used to initialize the data member, and the member without the initial value is initialized by default. However, it is important to note that the initial value in the class is either on the right side of the equals sign, or in curly braces, remembering that parentheses cannot be used.

5. The Declaration of all members must be within the class, but the definition of the member function body can be either inside the class or outside the class. When defined outside the class, the function name needs to be preceded by the class name and the scope operator (::) to explicitly indicate that the function is a member function of the corresponding class.

6. A function defined inside a class is an implicit inline function.

7. You can define the definition object while defining the class

Class P{int A;int B ();} P1,P2;

You can also define the class before you define the object. It is important to note that, unlike the definition of objects such as structs in C, C + + allows the class name to be used directly without requiring the previous class or struct qualifier, whereas the qualifier in C is necessary, even part of the type name.

C + +: struct p{int a;int b;};

              p p1;

C:struct p{int a;int b;};

             struct p p1;

8. Use the access specifier to enhance the encapsulation of the class:

8.1. Members that are defined after the public specifier can be accessed throughout the program, and public defines the interface for the class.

8.2. A member that is defined after the private specifier can be accessed by a member function of the class, but cannot be accessed using the class's code access, and the private part encapsulates (hides) the implementation details of the class.

8.3. A member defined after protected is called a protected member that can be accessed only in that class and in the class's derived classes (subclasses).

8.3. The access specifier has precedence over the default access attributes using class and struct.

8.4. A class can contain any number of access specifiers.

9. When defining a method (member function) of a class, if you do not need to modify the data members of the class in the method, it is recommended that you use the Const keyword after the method declaration and the parameter list that is defined to indicate that the user can no longer modify the data members of the class in that method, which is called the solid member function. A constant object, as well as a reference or pointer to a constant object, can only call a constant member function.

Remember, the const here is a top-level const, that is, if the class contains pointer members, then this pointer is a constant light pointer, cannot change the point, but can change the value of the indicated element.

Class P

{

     int a(int a,int b) const;

};

int a (int a,int b) const

{

     //不能修改类成员的值

}

Class in C + + and spinach platform Dragon

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.