Concept summary of the class:
Class is actually a special kind of structure (can be understood for the moment), which can contain functions and variables, except that the class has data hiding and abstraction, the class has private (private, can only be seen in the current class, the general case of data members are private, This state is a default access control for a class, and protected, as well as public, is the common interface member of the class, and is the part that can be called directly when the class is instantiated. In general, we do not define functions in the declaration of a class, and we generally place the declaration of the class in the header file, and if a function is defined in the class, it is automatically converted to an inline function (we can also define an inline function outside the class to add incline before the function declaration). Keyword with the rules defined in the class, we define the function with " :: " Using the header file of the Inlude class declaration, such as defining the function int Add (int a,int b) in the class, then we define the int p:: AD in the Class (class name is P). d (int a,int b) {...}, it is not strange to say that we are not unfamiliar with the elements in the namespace when they are called. we can assign a class object directly to another homogeneous object (just as the struct is), and the object wants to use the same method as the struct . " "operator, such as Stack.add ();
Constructors and destructors for classes:
The constructor is actually initialized for the object instantiated by the class, and when the constructor is not created in a class, the constructor is automatically provided when instantiated (the implicit version of the default constructor), but if you create a constructor with an input parameter, You must provide a default constructor for this class (that is, a constructor without parameter input), and the definition of the constructor (the function name is the same as the name of the class and there is no return value):
1 // The default constructor, for example, the name of the class is stock 2 Stock::stock () 3 {4// initialization of private data inside 5} 6 // with the parameter constructor, that is, the user can specify the size of the Privayte data
If the constructor is responsible until the object is created, then the destructor is to keep track of the object until it expires, and when it expires, it automatically calls a special member function that is responsible for cleaning up the function:
1 // Destructors 2 stock::~stock ()3{4// If you use new, delete the allocated memory 5 }
Const member functions:
When the definition of a class is a const, it cannot call the class method arbitrarily, because the class method is a way to modify the data in the class, so the compiler is not sure if you will use the class method to modify, so the error, this time the object can only call the const member function (that is, only the internal data is read and not modified functions) It is defined as follows:void Stock:: Show () const;
This pointer:
The this pointer is actually a pointer to the address of the class itself, as if you were in a house where you would see a lot of things in the house but you could not see the house itself, and this pointer is the function that this pointer always points to in the class itself
1 //This method is to compare the size of the hidden data of two objects, and finally return a relatively large object2 ConstStock & Stock::topval (ConstStock & S)Const3 {4 //because it is in a class method, you can call private data, whether it is an external object or this object5 if(s.total_val>total_val)6 returns; 7 Else8 return* This;9}The creation of constants in a class:
Since declaring a class simply describes the form of an object and does not create an object, there is no space for storage before it is created, and there are two ways to assign values to hidden data: 1. Implemented by a class method. 2. It is implemented by a constructor function. However, there are two ways to implement constants in a class that are created:
1. By declaring enumerations: enum {months=12}; Then you can use months instead of 12
2. Add static before the normal constant declaration. For example: static const int MONTH2=12, which is stored with other static variables and not stored in the object.
C + + Class