C++:构造函数、析构函数、拷贝构造函数

一、构造函数(constructor)

构造函数是一种特殊的成员函数,不需要函数调用语句,在创建对象时由系统自动调用。构造函数的作用是在对象被创建时使用特定的值去构造对象,使得在创建对象时就能够自动地完成对象的初始化。

  1. 名称与类名相同;
  2. 不能有任何返回类型,包括void类型;
  3. 参数可有可无,通过函数参数的不同来区别不同的构造函数;

Code

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

class Time{
    
    
   private:
   	int H; //时
   	int M; //分
   	int S; //秒
   public:
   	Time(){
    
    
   		cout << "Time() be called!" << endl;
   	}
   	Time(int H, int M, int S){
    
    
   		this -> H = H;
   		this -> M = M;
   		this -> S = S;
   		cout << "Time(int H, int M, int S) be called!" << endl;
   	}
};

int main(){
    
    
   Time T1;
   Time T2(10, 10, 10); //通过参数的不同来调用不同的构造函数
   return 0;
}

Result

Time() be called!
Time(int H, int M, int S) be called!

构造函数使用初始化列表进行初始化

Time::Time(int h, int m, int s): H(h), M(m), S(s){
    
    };

二、析构函数(destructor)

析构函数也是一种特殊的成员函数,在对象的生存期即将结束时被系统自动调用。析构函数的作用与构造函数相反,用来在对象被删除前做一些清理善后工作和数据保存工作。如利用delete运算释放用new运算分配地内存单元。

  1. 名称是在类名前加上符号“~”;
  2. 不能有任何返回类型,包括void类型;
  3. 不能有参数;

Code

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

class Time{
    
    
   private:
   	int H; //时
   	int M; //分
   	int S; //秒
   public:
   	Time(){
    
    
   		cout << "Time() be called!" << endl;
   	}
   	~Time(){
    
    
   		cout << "The destructor be called!" << endl;
   	}
};

int main(){
    
    
   Time T1;
   return 0;
}

Result

Time() be called!
The destructor be called!

注意,如果在定义类时没有定义构造函数和析构函数,系统也会自动为类添加一个默认的构造函数和一个默认的析构函数。默认的构造函数和析构函数都不带参数,执行空操作。如果定义了构造函数和析构函数,则系统不会再添加默认的构造函数和析构函数。

三、构造函数与析构函数的调用顺序

Code

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

class Time{
    
    
   private:
   	int H; //时
   	int M; //分
   	int S; //秒
   public:
   	Time(int, int, int); //构造函数
   	~Time(); //析构函数
};

Time::Time(int h, int m, int s){
    
     //"::"作用域限定符
   this -> H = h;
   this -> M = m;
   this -> S = s;
   cout << "The constructor is called:" << H << ":" << M << ":" << S << endl;
}

Time::~Time(){
    
    
   cout << "The destructor is called:" << H << ":" << M << ":" << S << endl;
}

int main(){
    
    
   Time T1(1, 1, 1);
   Time T2(10, 10, 10);
   return 0;
}

Result

The constructor is called:1:1:1
The constructor is called:10:10:10
The destructor is called:10:10:10
The destructor is called:1:1:1
//析构函数的调用顺序与构造函数的定义顺序相反,因为系统在栈中为对象分配内存

四、拷贝构造函数(copy constructor)

拷贝构造函数是一种特殊的构造函数,用来完成基于对象的同一类其它对象的构造及初始化。由于拷贝构造函数要把一个已有对象的数据成员赋值给新创建的对象,拷贝构造函数只有一个函数参数,本类的对象引用。

Code

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

class Time{
    
    
   private:
   	int H; //时
   	int M; //分
   	int S; //秒
   public:
   	Time(int, int, int); //构造函数
   	~Time(); //析构函数
   	Time(Time& t){
    
     //拷贝构造函数
   		H = t.H;
   		M = t.M;
   		S = t.S;
   		cout << "The copy constructor is called:" << H << ":" << M <<":" << S << endl; 
   	}
};

Time::Time(int h, int m, int s){
    
     //"::"作用域限定符
   this -> H = h;
   this -> M = m;
   this -> S = s;
   cout << "The constructor is called:" << H << ":" << M << ":" << S << endl;
}

Time::~Time(){
    
    
   cout << "The destructor is called:" << H << ":" << M << ":" << S << endl;
}

int main(){
    
    
   Time T1(1, 1, 1);
   Time T2 = T1;
   return 0;
}

Result

The constructor is called:1:1:1
The copy constructor is called:1:1:1
The destructor is called:1:1:1
The destructor is called:1:1:1

五、构造函数与拷贝构造函数

Code

#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;

class Time{
    
    
   private:
   	int H; //时
   	int M; //分
   	int S; //秒
   public:
   	Time(int h = 0, int m = 0, int s = 0){
    
    
   		H = h;
   		M = m;
   		S = s;
   		cout << "The constructor is called:" << H << ":" << M <<":" << S << endl; 
   	}
   	Time(Time& t){
    
    
   		H = t.H;
   		M = t.M;
   		S = t.S;
   		cout << "The copy constructor is called:" << H << ":" << M <<":" << S << endl; 
   	}
   	int getH(){
    
     return H; }
   	int getM(){
    
     return M; }
   	int getS(){
    
     return S; }
};

void func1(Time& T){
    
    
   cout << T.getH() << ":" << T.getM() << ":" << T.getS() << endl;
}

Time func2(){
    
    
   Time T(10, 10, 10);
   return T;
}

int main(){
    
    
   Time T1(1, 1, 1); //自动调用构造函数
   func1(T1); //对象作为函数参数,自动调用拷贝构造函数
   Time T2; //自动调用构造函数,用默认值初始化对象
   func1(T2); //自动调用拷贝构造函数
   T2 = func2(); //对象作为返回值,自动调用拷贝构造函数
   func1(T2); //自动调用拷贝构造函数
   return 0;
}

Result

The constructor is called:1:1:1
1:1:1
The constructor is called:0:0:0
0:0:0
The constructor is called:10:10:10
10:10:10

猜你喜欢

转载自blog.csdn.net/shangzhengyu/article/details/113008127