C ++ learns constructor and constructor functions.
In the C ++ class, there will be one or more constructor, one destructor, and one value assignment operator. Even if the classes we define do not display and define them, the compiler will declare a default constructor, A destructor, and a value assignment operator. For example:
1 // declare an Empty class 2 class Empty {}; 3 4 // but this Empty class is equivalent to the following class 5 class Empty 6 {7 Empty () {.....}; // default constructor 8 Empty (const Empty & rhs ){......} // copy the constructor 9 ~ Empty (){..........} // destructor 10 11 Empty operator * (const Empty & rhs ){.........} 12 };
It is worth noting that only when these functions are called will they be created by the compiler.
If we have already declared a constructor, the compiler will not create another default constructor.
The following describes constructor and destructor in detail.
1. Constructor
Constructor is a special member function with the same name as a class. It is mainly used to initialize data members of an object. The definition format is as follows:
1 class X2 {3 //....... 4 X (); // No parameter constructor 5 X (......); // constructor with parameters. constructor supports reloading 6 7 //......... 8}
The constructor declaration and definition methods are the same as other member functions of the class. You can define constructor within the class or declare the constructor in the class first, then define it outside the class. The form of a constructor defined outside the class is as follows:
1 X :: X(.....)2 {3 //...........4 }
Constructor has the following features:
(1) constructor and class have the same name
(2) The constructor does not return any type, and neither does void.
(3) constructors can be overloaded.
(4) The constructor is automatically called by the system and cannot be displayed in the program.
(5) the time to call the constructor is the first time after the object is defined, that is, the constructor is the first called function of the object.
(6) When defining an object array or using new to create a dynamic object, you must also call the constructor. However, when defining an array object, there must be no parameter Constructor
(7) constructor should usually be defined as a common member (of course, it can also be defined as private, but cannot be accessed by external class. The Singleton mode uses private constructors)
Pay attention to the following points:
(1) The initialization order of the members in the constructor initialization list is the same as the order they declare in the class and is irrelevant to the order in the initialization list. For example:
1 Tdate: Tdate (int m, int d, int y): month (m), day (d), year (y) {} 2 Tdate :: tdate (int m, int d, int y): year (y), month (m), day (d) {} 3 Tdate: Tdate (int m, int d, int y): day (d), year (y), month (m) {}4 // The constructors are identical.
(2) The constructor initialization list is executed before the statements in the constructor.
(3) The following class members must use the member initialization list for initialization: constant Member, reference Member, Class Object member, and derived class constructor calls the base class constructor.
1.1 default constructor
The default constructor does not need to display the constructor that provides parameters. In some cases, the default constructor must be used to define objects (such as object arrays)
If a class does not define any constructor, the compiler will generate a default constructor for it as needed. It is only responsible for creating objects without any initialization work.
When using the default constructor to create an object, if a global or static object is created, the bitwise mode of the object is all 0 (it can be understood that all data members are initialized to 0 ); if you create a local object and do not initialize the object data member, the object data member is unknown.
Note: The system generates a default constructor only when the class does not define any constructor. Once any form of constructor is defined, the system will no longer generate default constructor.
We can redefine the default constructor to provide the initial value for the data member of the object. We can also provide default values for parameters.
1.2 overload Constructors
The overloaded constructors must have different function prototypes (I .e., the number of parameters, parameter types, or Parameter order cannot be exactly the same)
1.3 copy constructor
A copy constructor is a special constructor used to initialize a new object based on an existing object.
If no replication constructor is defined for a class, the C ++ compiler will generate a minimal functionDefault copy constructorFormat: X (const X &){}
Default copy constructorMember replication is implemented in bit-by-bit mode. The value of each data member of an object is copied to the target object as is. The default constructor works well when no data member of the pointer type is involved. However, when a class has a data member of the pointer type, the default copy constructor usually produces a pointer suspension problem. If a data member of the pointer type exists in the class, a custom copy constructor (that is, a reload) should be provided for the class)
Note:: The parameters of the copy constructor are often references to const-type objects of this class (X: X (const X &){})
2. destructor
The destructor is another special member function with the same name as the class. It acts opposite to the constructor and is used to complete object cleanup at the end of the object's lifetime.
The name of the Destructor is changed from "~" + The "Class Name" is composed of the following forms:
1 class X 2 {3 private: 4 // ...... 5 public: 6 ~ X (); // destructor 7 X (); // No parameter constructor 8 X (....); // constructor 9 with parameters overloaded //......... 10}
Destructor have the following features::
(1) The Destructor name is "~" before the class name, It cannot be another name.
(2) The Destructor has no return value type (neither void nor parameter table ).
(3) destructor cannot be overloaded. A class can have only one destructor.
(4) The Destructor can only be automatically called by the system. It cannot be displayed in the program to call the destructor.
(5) If multiple objects end at the same time, C ++ calls the destructor in the reverse order of calling the constructor.
(6) each class should have a destructor. If no definition destructor is displayed, C ++ will generate a minimal default destructor (X ::~ X ())
(7) constructor and destructor can both be inline functions.
(8) In general, the Destructor should be set as a public member.