1, Function: The constructor is mainly used to complete the initialization of the member work
2, writing: the function with the same name as the class, generally public , in the case of a singleton can be defined as private, the constructor can take parameters , can also be overloaded , but there is no return type , Constructors may not be supplied manually.
3. Inheritance:
(1) When a base class does not have a constructor defined:
Derived classes can also not define constructors, all with the default constructors.
If only the derived class defines the constructor, you only need to construct the derived class object, and the base class part of the object is automatically created using the default constructor.
(2) A constructor exists in the base class:
A constructor with no parameters exists in the base class. If a base class defines a constructor, and the constructor has no arguments, it can be implicitly inherited, that is, the derived class does not need constructors at all.
A constructor with parameters exists in the base class. If the base class defines a constructor with parameters, the constructor must be inherited and implemented in the derived class. It also provides a way to pass parameters to the base class constructor so that the required data can be obtained when the base class is initialized, using the base keyword when implementing the constructor. After you create an object of a derived class, the program first runs the contents of the base class's constructor, and then the contents of the derived class.
4. copy constructor:
Special constructor , the name of the function must be the same as the class name, which One parameter that must be a reference variable of this type A copy constructor is a constructor that exists on the class itself.
Shallow copy deep copy : The default copy constructor has been able to handle most of the problem, the principle is that the corresponding variable is assigned, but there is a problem, when there are pointers or dynamic members in the variable, the copy pointer may not be the result we want, This is a shallow copy. in the case of a "deep copy", for dynamic members in an object, you cannot simply assign a value, but you should re- provision the space dynamically.
C + + constructor essentials