
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策制定
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
- C++ 檔案處理
- C++ 檔案和流
- C++ 從檔案中讀取
C++ 類建構函式和解構函式
類建構函式
類建構函式是類的特殊成員函式,每當我們建立該類的新的物件時都會執行。
建構函式的名稱與類名完全相同,並且根本沒有任何返回型別,甚至沒有 void。建構函式對於設定某些成員變數的初始值非常有用。
示例
下面的例子解釋了建構函式的概念:
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // This is the constructor private: double length; }; // Member functions definitions including constructor Line::Line(void) { cout << "Object is being created" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // Main function for the program int main() { Line line; // set line length line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
當上面的程式碼編譯並執行時,它會產生以下結果:
Object is being created Length of line : 6
引數化建構函式
預設建構函式沒有任何引數,但是如果需要,建構函式可以有引數。這有助於您在建立物件時為其分配初始值。
示例
以下示例演示了引數化建構函式的使用
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(double len); // This is the constructor private: double length; }; // Member functions definitions including constructor 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; } // Main function for the program int main() { Line line(10.0); // get initially set length. cout << "Length of line : " << line.getLength() <<endl; // set line length again 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 : 6
使用初始化列表初始化欄位
對於引數化建構函式,您可以使用以下語法初始化欄位:
Line::Line( double len): length(len) { cout << "Object is being created, length = " << len << endl; }
以上語法等同於以下語法:
Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; }
如果對於類 C,您有多個需要初始化的欄位 X、Y、Z 等,則可以使用相同的語法,並用逗號分隔欄位,如下所示:
C::C( double a, double b, double c): X(a), Y(b), Z(c) { .... }
類解構函式
解構函式是類的特殊成員函式,每當它的類物件超出作用域或每當將 delete 表示式應用於指向該類物件的指標時都會執行。
解構函式的名稱與類名完全相同,前面帶有波浪號 (~),它既不能返回值,也不能接受任何引數。解構函式對於在程式退出之前釋放資源(例如關閉檔案、釋放記憶體等)非常有用。
示例
以下示例解釋了解構函式的概念:
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // This is the constructor declaration ~Line(); // This is the destructor: declaration private: double length; }; // Member functions definitions including constructor 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; } // Main function for the program int main() { Line line; // set line length line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
當上面的程式碼編譯並執行時,它會產生以下結果:
Object is being created Length of line : 6 Object is being deleted
廣告