技術(shù)交流QQ群:1027579432,歡迎你的加入!
1.構(gòu)造函數(shù)
類的構(gòu)造函數(shù)是類的一種特殊的成員函數(shù),它會在每次創(chuàng)建類的新對象時執(zhí)行。構(gòu)造函數(shù)的名稱與類的名稱的完全相同的,并且不會返回任何數(shù)據(jù)類型,也不會返回void。構(gòu)造函數(shù)用于為某些成員變量設(shè)置初始值。
-
構(gòu)造函數(shù)實例如下:
#include "iostream" using namespace std; class Line{ public: void setLength(double len); double getLength(); Line(); // 構(gòu)造函數(shù) private: double length; }; // 構(gòu)造函數(shù)的定義 Line::Line(){ cout << "Line的對象line正在被創(chuàng)建..." << endl; } // 成員函數(shù)的定義 void Line::setLength(double len){ length = len; } double Line::getLength(){ return length; } int main(){ Line line; // 創(chuàng)建一個line對象,此時會去執(zhí)行構(gòu)造函數(shù) line.setLength(4.0); cout << "Line的長度是: " << line.getLength() << endl; return 0; } -
帶參數(shù)的構(gòu)造函數(shù):默認的構(gòu)造函數(shù)是沒有任何參數(shù)的,但如果需要參數(shù),構(gòu)造函數(shù)也可以帶有參數(shù)。這樣在創(chuàng)建對象時就會給對象賦初始值,如下面的例子:
#include "iostream" using namespace std; class Line{ public: void setLength(double len); double getLength(); Line(double len); // 帶參數(shù)的構(gòu)造函數(shù) private: double length; }; // 構(gòu)造函數(shù)的定義 Line::Line(double len){ cout << "Line的對象line正在被創(chuàng)建, lenght = " << len << endl; length = len; } // 成員函數(shù)的定義 void Line::setLength(double len){ length = len; } double Line::getLength(){ return length; } int main(){ // 獲取默認設(shè)置的長度 Line line(10.0); // 創(chuàng)建一個line對象,此時會去執(zhí)行帶參數(shù)的構(gòu)造函數(shù) line.setLength(4.0); cout << "Line的長度是: " << line.getLength() << endl; return 0; } -
使用初始化列表來初始化字段,見下面的實例:
class Box{ public: void setLength(double len); double getLength(); void setWidth(double wid); double getWidth(); void setHeight(double hei); double getHeight(); // 使用初始化列表來初始化字段----構(gòu)造函數(shù) Box(double len, double wid, double hei): length(len), width(wid), height(hei){ cout << "Box的對象box正在被創(chuàng)建,length = " << len << " width = " << wid << " height = " << hei << endl; } private: double length; double width; double height; }; // Box類的成員函數(shù)定義 void Box::setLength(double len){ length = len; } double Box::getLength(){ return length; } void Box::setWidth(double wid){ width = wid; } double Box::getWidth(){ return width; } void Box::setHeight(double hei){ height = hei; } double Box::getHeight(){ return height; } Box box(3.0, 4.0, 5.0); box.setLength(33.0); cout << "Box的長度是: " << box.getLength() << endl; box.setWidth(44.0); cout << "Box的寬度是: " << box.getWidth() << endl; box.setHeight(55.0); cout << "Box的高度是: " << box.getHeight() << endl; -
假設(shè)一個類C,具有多個字段X,Y,Z等需要進行初始化,同理,可以使用上面的語法,只需要在不同的字段使用逗號進行分隔即可。如下面的實例:
C::C(double a, double b, double c): X(a), Y(b), Z(c){ 語句; }
2.析構(gòu)函數(shù)
-
類的析構(gòu)函數(shù)的類的一種特殊的成員函數(shù),它會在每次刪除所創(chuàng)建的對象時執(zhí)行。析構(gòu)函數(shù)的名稱與類的名稱完全相同。只是在前面加上一個波浪號~作為前綴,它不會返回任何值,也不能帶有任何參數(shù)。析構(gòu)函數(shù)有助于跳出程序前釋放內(nèi)存資源。實例如下:
#include "iostream" using namespace std; class Line{ public: void setLength(double len); double getLength(); Line(double len); // 帶參數(shù)的構(gòu)造函數(shù) ~Line(); // 析構(gòu)函數(shù)不能帶有任何參數(shù) private: double length; }; class Box{ public: void setLength(double len); double getLength(); void setWidth(double wid); double getWidth(); void setHeight(double hei); double getHeight(); // 使用初始化列表來初始化字段----構(gòu)造函數(shù) Box(double len, double wid, double hei): length(len), width(wid), height(hei){ cout << "Box的對象box正在被創(chuàng)建,length = " << len << " width = " << wid << " height = " << hei << endl; } // 析構(gòu)函數(shù) ~Box(){ cout << "對象box正在被刪除...\n"; } private: double length; double width; double height; }; // 構(gòu)造函數(shù)的定義 Line::Line(double len){ cout << "Line的對象line正在被創(chuàng)建, lenght = " << len << endl; length = len; } // 析構(gòu)函數(shù)定義 Line::~Line(){ cout << "Line的對象正在被刪除..." << endl; } // // 使用初始化列表來初始化字段 // Line::Line(double len): length(len){ // cout << "Line的對象line正在被創(chuàng)建, lenght = " << len << endl; // } // 成員函數(shù)的定義 void Line::setLength(double len){ length = len; } double Line::getLength(){ return length; } // Box類的成員函數(shù)定義 void Box::setLength(double len){ length = len; } double Box::getLength(){ return length; } void Box::setWidth(double wid){ width = wid; } double Box::getWidth(){ return width; } void Box::setHeight(double hei){ height = hei; } double Box::getHeight(){ return height; } int main(){ // 獲取默認設(shè)置的長度 Line line(10.0); // 創(chuàng)建一個line對象,此時會去執(zhí)行帶參數(shù)的構(gòu)造函數(shù) line.setLength(4.0); cout << "Line的長度是: " << line.getLength() << endl; cout << "-------------------------------------------\n"; Box box(3.0, 4.0, 5.0); box.setLength(33.0); cout << "Box的長度是: " << box.getLength() << endl; box.setWidth(44.0); cout << "Box的寬度是: " << box.getWidth() << endl; box.setHeight(55.0); cout << "Box的高度是: " << box.getHeight() << endl; return 0; }- 注意:上面的程序中,創(chuàng)建了兩個對象line和box,由于是先創(chuàng)建的line對象,后創(chuàng)建的是box對象;所以在執(zhí)行析構(gòu)函數(shù)時,必須先刪除box對象,后刪除line對象。
- C++初始化類成員時,是按照聲明的順序初始化的,而不是按照出現(xiàn)在初始化列表中的順序!,見下面的實例:
class Student1{
public:
int a;
int b;
// 普通成員函數(shù)
void fprint(){
cout << "a = " << a << ",b = " << b << endl;
}
// Student1(int i): b(i), a(b){} 異常順序:發(fā)現(xiàn)a的值是0,b的值是2;說明初始化僅僅對b有效,對a沒有起到初始化的作用
Student1(int i): a(i), b(a){} // 正常順序:a = b = 2
// 默認構(gòu)造函數(shù)
Student1(){
cout << "默認構(gòu)造函數(shù)Student1\n";
}
// 拷貝構(gòu)造函數(shù)
Student1(const Student1& t1){
cout << "拷貝構(gòu)造函數(shù)Student1\n";
this->a = t1.a;
}
};
Student1 A(2); //進入默認構(gòu)造函數(shù)
A.fprint(); //輸出前面初始化的結(jié)果