Cplusplus-cpp-constructor-destructor

提供:Dev Guides
移動先:案内検索

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) {
   ....
}

クラスデストラクタ

  • デストラクタ*は、クラスのオブジェクトがスコープ外に出たとき、またはそのクラスのオブジェクトへのポインタに削除式が適用されたときに実行されるクラスの特別なメンバー関数です。

デストラクタは、チルダ(〜)を前に付けたクラスとまったく同じ名前を持ち、値を返すことも、パラメータを取ることもできません。 デストラクタは、ファイルを閉じる、メモリを解放するなど、プログラムから出る前にリソースを解放するのに非常に役立ちます。

次の例では、デストラクタの概念を説明します-

#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