Functions and characteristics of constructor and destructor (revolution)

Functions and characteristics of
constructor and destructor Constructor and destructor are two special member functions described in the class body.
The function of the constructor is to initialize the object with the given value when it is created.
The function of the destructor is used to release an object. Before the object is deleted, use it to do some clean-up work, it is the opposite of the function of the constructor.

The characteristics of the constructor are as follows:
(1) The constructor is a member function, and the function body can be written in the class body or outside the class.
(2) The constructor is a special function, the name of the function is the same as the class name, the function does not specify the type description, it has an implicit return value, which is used internally by the system. The function can have one parameter or multiple parameters.
(3) The constructor can be overloaded, that is, multiple functions with different numbers of parameters can be defined.
(4) The constructor cannot be called directly in the program, and the system automatically calls the constructor when the object is created.
The characteristics of the destructor are as follows:
(1) The destructor is a special member function, its name is the same name, and the character "~" is added in front to distinguish it from the constructor. The destructor does not specify the data type and has no parameters.
(2) Only one destructor can be defined in a class, and the destructor cannot be overloaded.
(3) The destructor can be called or called by the system. In the following two cases, the destructor will be called automatically. One is that if an object is defined in a function body, when the function ends, the destructor of the object is automatically called; the other is that when an object is passively created using the new operator, it is released using the delete operator When, delete will automatically call the destructor.
If no constructor is defined when the class is defined, the compiler will automatically generate a default constructor without parameters, the format is as follows:
class name::default constructor name ()
{}
According to the provisions of the constructor, the default constructor name is the same name. This format of the default constructor can also be defined in the class body by the programmer. Define an object in the program without specifying initialization, the compiler will initialize the object according to the default constructor, and all data members of the object are initialized to zero or empty.
Similarly, if there is no destructor defined in a class, the compiler system will also generate a default destructor, the format is as follows:
class name:: ~ default destructor name ()
{ } default destructor The name is also the same name, the default destructor is an empty function

Guess you like

Origin blog.csdn.net/qq_14874791/article/details/107653442