Constructor: Used to initialize data members in the class
The system automatically generates the default constructor (the parameter is blank). However, if you manually define a constructor with parameters, the system automatically overwrites the default constructor and calls the default constructor for instantiation, you need to manually define a constructor without Parameters
Initialization list: class data members can be initialized by initializing the list, for example, defining the initialization list: User (): sId (0001), sSex (1)
When the base class is inherited, the base class constructor is called in the derived class to initialize the base class, which is actually the initialization list.
Constructors can be declared only in the class body, defined in the class body, and do not need to initialize the list during the Declaration. The initialization list (if any) needs to be provided during the definition ).
You can initialize the list in either of the following situations:
- The data member has a constant Member: const int Id;
- The data member has sub-objects, and the class contains another Class Object: Group group1;
The execution sequence of the constructor is as follows:
- Allocate memory
- Initialize member list
- Execute the constructor
Constant members cannot be placed in constructors: they cannot be modified after being declared, and can only be placed in the initialization list;
Sub-Object: To define a class object, you must first allocate memory. If another class object is included, you must also allocate memory for it. Therefore, initialization of other class objects must be prior to the constructor;
The data members of the class and the member functions are stored separately. The data members are stored in the stack area, and the member functions are stored in the code area. The member functions use the this pointer to access their own objects.
Static members:
Static members are stored in the static global zone, which is not closely related to the class. The lifetime of the static member is the entire running period, and the process is released only after the process ends!
Static members must be defined in the external class (initialized in the constructor) or initialized before they can be used.
Static and non-static mutual access:
After testing, constructors can also initialize static members. Of course, other member functions can also operate static members.
However, for a static member function to access non-static members, special processing is required: pass a specific class object to the static member function, and then access non-static members through this class object.
Common member functions: If a member function is modified with const, internal members cannot be modified. this is equivalent to adding const to the internal this pointer and cannot be modified using this-> id =.
Destructor: The Destructor does not delete objects, but clears the memory space occupied by objects before they are deleted.
The system automatically generates an empty destructor.
Purpose: Use destructor to prevent memory leakage when internal pointers exist.
The Destructor is automatically executed for general objects, such as User users, out of scope.
But for dynamically allocated objects: User user = new user; The Destructor is not automatically executed even if the scope is exceeded. You must manually delete the user to call the destructor.
Static local objects are not released at the end of the function call, nor are they called. The static object destructor is called only when the main function ends or the exit statement is executed.
The Destructor can also be defined in the class body, and the function declaration is within the class body;
Destructor execution sequence:
For objects of the same type, the first constructor is constructed, and then the first constructor is constructed.
Cause: because the object exists in the stack and comes out first.
1 class CMyClass 2 { 3 public: 4 CMyClass():iNo(0) 5 {} 6 CMyClass(int i):iNo(i) 7 { 8 this->iId = i; 9 CMyClass();10 }11 public:12 int iId;13 const int iNo;14 };
Constructor: automatically executes constructor when an object is created.
CMyClass myObject; // automatically executes the constructor.
Constructors with parameters:
CMyClass myObject (1); // automatically execute the constructor
One constructor can also call another constructor manually. However, if the called constructor Declaration contains a list of initialization parameters, the parameter initialization list is not executed at this time.
The parameter initialization list is executed only when the system is automatically called.
In the above example, MyClass () is called in CMyClass (int I), and the initialization list iNo (0) is not executed );
Sequence of calling constructor and destructor:
1. Global Object: the constructor is called before all functions in the file are executed. The main function is executed or the Destructor is executed when exit is called.
2. Static local: in a program, the constructor is called once when the function where the static local object is located. The constructor is not released after the call, execute the Destructor after the main function is executed or when exit is called.
3. Partial Automatic Object: when an object is created, its constructor is called. When the object in which the function is executed is released, its destructor is called.