構造函數、初始化列表來初始化字段、析構函數、拷貝構造函數
阿新 • • 發佈:2018-05-20
eve 字段 pub ble setlength div 另一個 類型 src
#include <iostream> using namespace std; class Line { public: int getLength( void ); Line( int len ); // 簡單的構造函數 Line( const Line &obj); // 拷貝構造函數 ~Line(); // 析構函數 private: int *ptr; }; // 成員函數定義,包括構造函數View CodeLine::Line(int len) { cout << "調用構造函數" << endl; // 為指針分配內存 ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "調用拷貝構造函數並為指針 ptr 分配內存" << endl; ptr = new int; *ptr = *obj.ptr; // 拷貝值 } Line::~Line(void) { cout<< "釋放內存" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "line 大小 : " << obj.getLength() <<endl; } // 程序的主函數 int main( ) { Line line(10); display(line); return 0; } //結果 調用構造函數 調用拷貝構造函數並為指針 ptr 分配內存 line 大小 :10 釋放內存 釋放內存
1、類的構造函數是類的一種特殊的成員函數,它會在每次創建類的新對象時執行。
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // 這是構造函數 private: double length; }; // 成員函數定義,包括構造函數 Line::Line(void) { cout << "Object is being created" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函數 int main( ) { Line line; // 設置長度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; } //結果 Object is being created Length of line : 6View Code
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(double len); // 這是構造函數 private: double length; }; // 成員函數定義,包括構造函數 Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函數 int main( ) { Line line(10.0); // 獲取默認設置的長度 cout << "Length of line : " << line.getLength() <<endl; // 再次設置長度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; } //結果 Object is being created, length = 10 Length of line : 10 Length of line : 6View Code
2、構造函數使用初始化列表來初始化字段
Line::Line( double len): length(len) { cout << "Object is being created, length = " << len << endl; } //上面的語法等同於如下語法: //在構造函數上對變量length進行賦值操作 Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; }View Code
3、類的析構函數是類的一種特殊的成員函數,它會在每次刪除所創建的對象時執行。析構函數的名稱與類的名稱是完全相同的,只是在前面加了個波浪號(~)作為前綴,它不會返回任何值,也不能帶有任何參數。析構函數有助於在跳出程序(比如關閉文件、釋放內存等)前釋放資源。
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // 這是構造函數聲明 ~Line(); // 這是析構函數聲明 private: double length; }; // 成員函數定義,包括構造函數 Line::Line(void) { cout << "Object is being created" << endl; } Line::~Line(void) { cout << "Object is being deleted" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函數 int main( ) { Line line; // 設置長度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }View Code
4、拷貝構造函數是一種特殊的構造函數,它在創建對象時,是使用同一類中之前創建的對象來初始化新創建的對象。拷貝構造函數通常用於:
-
通過使用另一個同類型的對象來初始化新創建的對象。
-
復制對象把它作為參數傳遞給函數。
-
復制對象,並從函數返回這個對象。
如果在類中沒有定義拷貝構造函數,編譯器會自行定義一個。如果類帶有指針變量,並有動態內存分配,則它必須有一個拷貝構造函數。拷貝構造函數的最常見形式如下:
構造函數、初始化列表來初始化字段、析構函數、拷貝構造函數