构造函数是一种特殊的成员函数,不需要函数调用语句,在创建对象时由系统自动调用。构造函数的作用是在对象被创建时使用特定的值去构造对象,使得在创建对象时就能够自动地完成对象的初始化。
#include
#include
#include
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;
}
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){};
析构函数也是一种特殊的成员函数,在对象的生存期即将结束时被系统自动调用。析构函数的作用与构造函数相反,用来在对象被删除前做一些清理善后工作和数据保存工作。如利用delete运算释放用new运算分配地内存单元。
#include
#include
#include
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;
}
Time() be called!
The destructor be called!
注意,如果在定义类时没有定义构造函数和析构函数,系统也会自动为类添加一个默认的构造函数和一个默认的析构函数。默认的构造函数和析构函数都不带参数,执行空操作。如果定义了构造函数和析构函数,则系统不会再添加默认的构造函数和析构函数。
#include
#include
#include
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;
}
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
//析构函数的调用顺序与构造函数的定义顺序相反,因为系统在栈中为对象分配内存
拷贝构造函数是一种特殊的构造函数,用来完成基于对象的同一类其它对象的构造及初始化。由于拷贝构造函数要把一个已有对象的数据成员赋值给新创建的对象,拷贝构造函数只有一个函数参数,本类的对象引用。
#include
#include
#include
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;
}
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
#include
#include
#include
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;
}
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