[C++] class constructor and destructor
1. Constructor
2. Constructor overloading
3. Destructor
refer to:
"C++ from entry to proficient" People's Posts and Telecommunications Publishing House
1. Constructor
The constructor is a function with the same name as the class, and its role is to initialize the object. The constructor is automatically called when the object is created.
Features:
1. No Type
2. No return value (and no need to write void)
3. The name is the same as the class name
4. Reloadable!
Role : complete the initialization of the object of the class
Cdate d; //Define object d
Note: When the object d is created, the constructor d.Cdate() is automatically called.
When no constructor is defined in a class, the compiler automatically assumes the following two default constructors: (This constructor does nothing, just a formality). If the author defines the constructor himself, the default constructor does not exist.
//default constructor one Cdate::Cdate() { } //default constructor two Cdate::Cdate(const Cdate& a) { }
Programming example:
//Constructor.cpp #include<iostream> using namespace std; class Cdate { public: Cdate(int ,int ,int ); //Constructor void print(); //member function private: int year,month,day; //member data }; // implementation of the constructor Cdate::Cdate(int x,int y,int z) //constructor { year=x; month=y; day=z; } // implementation of member functions void Cdate:: print() { cout<<year<<" "<<month<<" "<<day<<endl; } intmain() { Cdate d(2018,5,6); d.print(); return 0; }
operation result:
2. Constructor overloading
Like other normal functions, constructors can be overloaded multiple times (as long as each constructor has a different parameter type and number).
Cdate(); //Constructor takes no parameters Cdate(int ,int ,int ); //Constructor with parameters
When overloading, the compiler will automatically call the constructor with the same number and type of parameters as required.
Programming example:
//Constructor overload.cpp #include<iostream> using namespace std; class Cdate { public: Cdate(); //Constructor takes no parameters Cdate(int ,int ,int ); //Constructor with parameters void print(); //member function private: int year,month,day; //member data }; // implementation of the constructor Cdate::Cdate() //Constructor { year=1999; month=7; day=1; } // implementation of the constructor Cdate::Cdate(int x,int y,int z) //constructor { year=x; month=y; day=z; } // implementation of member functions void Cdate:: print() { cout<<year<<" "<<month<<" "<<day<<endl; } intmain() { Cdate d1(2018,5,6); cout<<"d1:" ; d1.print(); Cdate d2; cout<<"d2:"; d2.print(); return 0; }
operation result:
3. Destructor
We already know that constructors initialize objects when they are created. The destructor , on the other hand, is to perform cleanup work automatically by the system before the object is deleted .
As a class, there may be multiple objects, and the destructor is called at the end of each object's life, and once per object.
Features:
1. No type
2. No return value
3. The name is the same as the class name
4. No parameters, no overloading, only one destructor!
5. " ~ " before the destructor (the negation sign, which means the reverse constructor )
Role: do cleanup work before the object is deleted.
Note: The destructor of an object is called before the object is destroyed, and when the object is destroyed is also related to its scope.
For example, the global object is destroyed at the end of the program;
Automatic objects are destroyed when they leave their scope;
Dynamic objects are destroyed when the delete operator is used.
Destructors are especially useful when an object is dynamically allocated memory and you want to free the memory it occupies before the object is destroyed. We don't ignore the importance of initialization, but we often ignore the importance of cleanup, but memory cleanup for destroyed variables is very important.
For example, we have applied for some memory in the heap memory leaks , which will reduce the operating efficiency of the application, or even crash, which should not be taken lightly.
In C++, a destructor is provided to ensure that the object clearing work is performed automatically.
Programming example:
//destructor.cpp #include<iostream> using namespace std; class Cdate { public: Cdate(int ,int ,int ); //Constructor with parameters ~Cdate(); //destructor void print(); //member function private: int year,month,day; //member data }; // definition of the constructor Cdate::Cdate(int x,int y,int z) //constructor { year=x; month=y; day=z; cout<<"Implement constructor"<<endl; } // definition of destructor Cdate::~Cdate() //destructor { cout<<"The destructor releases the memory resources occupied by the variable"<<endl; } // implementation of member functions void Cdate:: print() { cout<<year<<" "<<month<<" "<<day<<endl; } intmain() { Cdate d1(2018,5,6); cout<<"d1:" ; d1.print(); return 0; }
operation result:
Analyze the working process of the constructor and destructor:
Cdate d1(2018,5,6); //Define object
The first is to automatically call the constructor to initialize the object when it is created
Finally, the destructor is executed before the object is deleted to clean up the memory occupied by the variable
Programming example of destructor calling order: Makes this process clearer.
//destructor.cpp #include<iostream> using namespace std; class Cdate { public: Cdate(int); //Constructor with parameters ~Cdate(); //destructor void print(); //member function private: int num; //member data }; // definition of the constructor Cdate::Cdate(int x) //Constructor { num=x; cout<<num<<":"; cout<<"Constructor is called"<<endl; } // definition of destructor Cdate::~Cdate() //destructor { cout<<num<<":"; cout<<"The destructor is called"<<endl; } // implementation of member functions void Cdate:: print() { cout<<num<<endl; } intmain() { cout<<"Enter the main() function:"<<endl; Cdate d1(1); Cdate d2(2); Cdate d3(3); cout<<"main() function is running:"<<endl; cout<<"Exit the main() function"<<endl; return 0; }
operation result:
This example shows that the order of invocation of destruction and construction is reversed, that is, the first constructed is destructed last, and the last constructed is destructed first.
------------------------------------------- END -------------------------------------