Constructor: Invoked when an object is constructed (display invocation and implicit invocation), only when the object is constructed, the corresponding construction method is selected according to the parameter type and number of the new object, and the method is called when there are no parameters, and the corresponding parametric construction method is called when there are parameters. The parameters of the constructor are typically used to initialize the data members of the class
Features and functions of constructors:
1. Constructors must be named exactly the same as the class name.
2. The function of a constructor is primarily used to define the state of initialization when the object of a class is created. It has no return value, nor can it be decorated with void.
3. Constructors cannot be called directly, and must be invoked automatically by the new operator when an object is created.
4. When a class definition is defined, a constructor is usually default, and this default constructor takes no arguments. When you customize the Include parameter constructor, the default constructor will need to be written manually ... (the default constructor is parameter-free, and the constructor you define can take parameters)
Show Call constructor
The stock food = the stock (14,250,65);
Function: The stock class object food is created, and the class member data is initialized by invoking its own defined stock class constructor.
Implicit call constructor Stock food (14,250,65);
Function: The stock class object food is created, and the class member data is initialized by invoking its own defined stock class constructor. The results of the two invocation methods are the same, but the second method is more compact.