C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++の仮想コピーコンストラクタ


トピックを深く掘り下げる前に、関連するすべての用語をブラッシュアップしましょう。

コピーコンストラクタ 渡されるオブジェクトの正確なコピーであるオブジェクトを作成するために使用される特殊なタイプのコンストラクターです。

仮想関数 親クラスで宣言され、親クラスを継承する子クラスで再定義(オーバーライド)されるメンバー関数です。

仮想コピーコンストラクターを使用すると、プログラマーはオブジェクトの正確なデータ型を知らなくてもオブジェクトを作成できます。

C ++プログラミング言語では、copy Constructorは、別のオブジェクトからコピーされたオブジェクトを作成するために使用されます。ただし、作成されたオブジェクトのタイプについてプログラムが実行時に決定するようにする場合、つまり、オブジェクトタイプはコンパイル時ではなく実行時に定義され、特定の条件に対してユーザーが提供する入力に基づいています。この状況では、このことを行うためにいくつかの特別な権限を持つコピーコンストラクターが必要です。そのため、この仮想コピーコンストラクターは、オブジェクトのクローン作成をリアルタイムで提供するように宣言されています。

例を見てみましょう。プログラムを使用して、そのエリアを特定する人物がいるとします。ただし、オブジェクトのタイプアップは、正方形の長方形または円にすることができるリアルタイムとして定義されます。そのため、ユーザーが入力したタイプに基づいてオブジェクトをコピーする仮想コピーコンストラクターを使用します。

仮想コンストラクターが正しく機能するためには、基本クラスに対して定義された2つのメソッドがあります。彼らは-

clone()
create()

コピーコンストラクターは仮想クローンメソッドを使用しますが、仮想作成メソッドは、仮想コンストラクターを作成するためのデフォルトのコンストラクターによって使用されます。

#include <iostream>
using namespace std;
class figure{
   public:
   figure() { }
   virtual
   ~figure() { }
   virtual void ChangeAttributes() = 0;
   static figure *Create(int id);
   virtual figure *Clone() = 0;
};
class square : public figure{
   public:
   square(){
      cout << "square created" << endl;
   }
   square(const square& rhs) { }
   ~square() { }
   void ChangeAttributes(){
      int a;
      cout<<"The side of square";
      cin>>a;
      cout<<"Area of square is "<<a*a;
   }
   figure *Clone(){
      return new square(*this);
   }
};
class circle : public figure{
   public:
   circle(){
      cout << "circle created" << endl;
   }
   circle(const circle& rhs) { }
   ~circle() { }
   void ChangeAttributes(){
      int r;
      cout << "enter the radius of the circle ";
      cin>>r;
      cout<<"the area of circle is "<<((3.14)*r*r);
   }
   figure *Clone(){
      return new circle(*this);
   }
};
class rectangle : public figure{
   public:
   rectangle(){
      cout << "rectangle created" << endl;
   }
   rectangle(const rectangle& rhs) { }
   ~rectangle() { }
   void ChangeAttributes(){
      int a ,b;
      cout<<"The dimensions of rectangle ";
      cin>>a>>b;
      cout<<"Area of rectangle is "<<a*b;
   }
   figure*Clone(){
      return new rectangle(*this);
   }
};
figure *figure::Create(int id){
   if( id == 1 ){
      return new square;
   }
   else if( id == 2 ){
      return new circle;
   }
   else{
      return new rectangle;
   }
}
class User{
   public:
   User() : figures(0){
      int input;
      cout << "Enter ID (1, 2 or 3): ";
      cin >> input;
      while( (input != 1) && (input != 2) && (input != 3) ){
         cout << "Enter ID (1, 2 or 3 only): ";
         cin >> input;
      }
      figures = figure::Create(input);
   }
   ~User(){
      if( figures ){
         delete figures;
         figures = 0;
      }
   }
   void Action(){
      figure *newfigure = figures->Clone();
      newfigure->ChangeAttributes();
      delete newfigure;
   }
   private:
   figure *figures;
};
int main(){
   User *user = new User();
   user->Action();
   delete user;
}

出力

Enter ID (1, 2 or 3): 2
circle created
enter the radius of the circle R 3
the area of circle is 28.26

  1. C++でのコンストラクターと代入演算子のコピー

    コピーコンストラクタと代入演算子は、あるオブジェクトを別のオブジェクトに初期化するために使用されます。それらの主な違いは、コピーコンストラクタが新しいオブジェクト用に個別のメモリブロックを作成することです。ただし、代入演算子は新しいメモリ空間を作成しません。参照変数を使用して、前のメモリブロックを指します。 コピーコンストラクタ(構文) classname (const classname &obj) { // body of constructor } 代入演算子(構文) classname Ob1, Ob2; Ob2 = Ob1; コピーコンストラクタと代入演算子の詳細な違

  2. C++でコンストラクタをコピーする

    コピーコンストラクターはコンストラクターの一種です。オブジェクトを作成し、同じクラスのオブジェクトで初期化します。コピーコンストラクターがクラスで定義されていない場合は、コンパイラー自体が定義します。コピーコンストラクタは、ポインタ変数または動的メモリ割り当てを持つクラスの必須事項です。 コピーコンストラクタを示すプログラムは次のとおりです。 例 #include<iostream> using namespace std; class Demo {    private:    int num1, num2;    p