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
廣告