C++ classes and objects (3)

1. The 6 default member functions of the class

If there are no members in a class, it is simply called an empty class.

Is there really nothing in the empty class? No, when any class does not write anything, the compiler will automatically generate the following 6 default member functions.

Default member function: The member function generated by the compiler without explicit implementation by the user is called the default member function.

insert image description here

2. Constructor

2.1. Concept (problem formulation)

For the following Date classes:
insert image description here
For the Date class, you can set the date for the object through the Init public method, but if you call this method to set the information every time you create an object, it is a bit troublesome, can you set the information when the object is created Woolen cloth?

The constructor is a special member function with the same name as the class name , which is automatically called by the compiler when creating a class type object to ensure that each data member has a suitable initial value, and is called only once in the entire life cycle of the object .

2.2. Features

The constructor is a special member function. It should be noted that although the name of the constructor is called construction, the main task of the constructor is not open space to create objects , but to initialize objects .

Has the following properties:

  1. The function name is the same as the class name.
  2. No return value.
  3. The compiler automatically calls the corresponding constructor when the object is instantiated.
  4. Constructors can be overloaded.
    insert image description here
  5. If the constructor is not explicitly defined in the class, the C++ compiler will automatically generate a default constructor without parameters. Once the user defines the definition, the compiler will no longer generate it.
    insert image description here
  6. About the default member function generated by the compiler: If the constructor is not implemented, the compiler will generate a default constructor. But it seems that the default constructor is useless? The d object invokes the compiler-generated default constructor. But the d objects _year, __month, _day are still random values. That is to say, the default constructor generated by the compiler is of no use here? ?
    Answer: C++ divides types into built-in types (basic types) and custom types. The built-in type is the data type provided by the language, such as: int/char..., the custom type is the type defined by us using class/struct/union, etc. If you look at the following program, you will find that the compiler generates the default ditch station The function calls its default member function on the custom type member _t.
    insert image description here

Note: In C++11, a patch has been patched for the defect of non-initialization of built-in type members, that is, built-in type member variables can be given default values ​​when declared in a class.
insert image description here

  1. Both the parameterless constructor and the default constructor are called default constructors, and there can only be one default constructor. Note: No-argument constructors, full default constructors, and constructors that we did not write to be generated by the compiler by default can all be considered default constructors. There is one and only one of these three default constructors.
    insert image description here

3. Destructor

3.1. Concept

Through the study of the previous constructor, we know how an object came about, and how did that object disappear?

Destructor: Contrary to the function of the constructor, the destructor does not complete the destruction of the object itself, and the local object destruction is done by the compiler. When the object is destroyed, it will automatically call the destructor to complete the cleanup of resources in the object.

3.2. Features

A destructor is a special member function whose characteristics are as follows:

  1. The destructor name is prefixed with the character ~ before the class name.
  2. No parameter return value type.
  3. A class can have only one destructor. If it is explicitly defined, the system will automatically generate a default destructor. Note: Xigou function cannot be overloaded.
  4. When the object life cycle ends, the C++ compilation system automatically calls the destructor.
    insert image description here
  5. Is there something done about the compiler's auto-generated constructor? In the following program, we will see that the default destructor generated by the compiler calls its destructor for the custom type members.
    insert image description here
  6. If there is no resource application in the class, the destructor can not be written, and the default destructor generated by the compiler can be used directly, such as the Data class; when there is a resource application, it must be written, otherwise it will cause resource leakage, such as the Stack class.

Guess you like

Origin blog.csdn.net/zxj20041003/article/details/130309020