[C++] Concepts and characteristics of constructors and default constructors

1. Constructor

1. Concept of constructor function

It is a common one among the six default functions. This function is automatically called when calling a class and is generally used for initialization.

2. Characteristics of constructor

① There is no function type and return value, and the function name is the same as the class name;

② Function overloading is possible;

③The parameters passed can have default values;

④If no constructor is created, a constructor will be automatically generated.

2. Default constructor

1. Concept of default constructor

A constructor automatically generated by the compiler without creating one yourself.

2. Characteristics of the default constructor

① Initialize members of built-in types, but members of custom types may not necessarily be initialized (depending on different versions of compilers);

② Generally, if there are built-in type members, you need to write the constructor yourself, and you cannot use the one generated by the compiler;

③All are custom type members, you can consider letting the compiler generate them by itself;

Guess you like

Origin blog.csdn.net/qq_74641564/article/details/130328908