Constructor
Constructor is a special member function It is executed every time a new object of the class is created The constructor name is exactly the same as the class name and does not return any type Constructors can be used to set initial values for some member variables
Format:
Class::Class(); // Constructor
Default constructor
If the user does not define a constructor, the C + + system will automatically generate a default constructor This constructor body is empty, has no parameters, and does not perform initialization
For example:
Time.h:
class Time { private: int hour; int minute; int second; public: Time(); // Default constructor void set_time(int h, int m, int s); void show_time(); };
Time.cpp:
#include "Time.h" #include <iostream> using namespace std; Time::Time() { hour = 0; minute = 0; second = 0; } void Time::set_time(int h, int m, int s) { hour = h; minute = m; second = s; } void Time::show_time() { cout << hour << ":" << minute << ":" << second << endl; }
main:
#include "Time.h" #include <iostream> using namespace std; int main() { Time time1; // Instantiate time time1.show_time(); // Call show_time return 0; }
Output result:
0:0:0
a key:
- Even if other constructors are provided, it is almost always right to provide a default constructor
- Usually, in the default constructor, the initial value provided to the member should indicate that the object is "empty"
Parameterized constructor
Parameters in the constructor can specify default values If Tong Hu does not specify the argument, compile Xiyong to make the formal parameter take the default value
For example:
Time class:
#ifndef PROJECT1_TIME_H #define PROJECT1_TIME_H class Time { private: int hour; int minute; int second; public: Time(); // Default constructor Time(int h, int m=0, int s=0); // Parameterized constructor void show_time(); }; #endif //PROJECT1_TIME_H
Time.cpp:
#include "Time.h" #include <iostream> using namespace std; // Default constructor Time::Time() : hour(0), minute(0), second(0) {} // Parameterized constructor Time::Time(int h, int m, int s) : hour(h), minute(m), second(s) {} void Time::show_time() { cout << hour << ":" << minute << ":" << second << endl; }
mian:
#include "Time.h" #include <iostream> using namespace std; int main() { Time time1; time1.show_time(); Time time2(8); time2.show_time(); Time time3(8, 8); time3.show_time(); Time time4(8, 8, 8); time4.show_time(); return 0; }
Output result:
0:0:0 8:0:0 8:8:0 8:8:8
Destructor
Destructor is also a special member function When the life of an object ends, the destructor is automatically executed The name of the destructor is the class name preceded by a "~" symbol
Format:
Class::~Class(); // Destructor
The function of the destructor is to clean up and deal with the aftermath before revoking the memory occupied by the object
Destructor example
Student class:
#ifndef PROJECT1_STUDENT_H #define PROJECT1_STUDENT_H #include <string> using namespace std; class Student { private: int num; string name; char gender; public: Student(); Student(int num, string name, char gender); ~Student(); void display(); }; #endif //PROJECT1_STUDENT_H
Student.cpp:
#include "Student.h" #include <iostream> using namespace std; // Nonparametric structure Student::Student() : num(-1), name("None"), gender('N') {} Student::Student(int n, string p, char g) : num(n), name(p), gender(g) { cout << "Execute constructor: " << "Welcome, " << name << endl; } void Student::display() { cout << "num: " << num << endl; cout << "name: " << name << endl; cout << "gender: " << gender << endl; cout << "===============" << endl; } Student::~Student() { cout << "Execute destructor: " << "Bye bye, " << name << endl; }
main:
#include "Student.h" #include <iostream> using namespace std; int main() { Student student1(1, "Little white", 'f'); Student student2(2, "Big white", 'f'); student1.display(); student2.display(); return 0; }
Output result:
Execute constructor: Welcome, Little white Execute constructor: Welcome, Big white num: 1 name: Little white gender: f =============== num: 2 name: Big white gender: f =============== Execute destructor: Bye bye, Big white Execute destructor: Bye bye, Little white
Destructor execution timing
For an automatic local object defined in a function, the object is released when the function is called The destructor is automatically executed before the object is released
Local object
Static local object only calls the post washing function of static local object when the main function ends or the exit function is called to end the program
Global object
For a global object, when the process of the program leaves its scope (such as the end of the main function or the call of the exit function), the destructor of the global object is called