Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++의 가상 복사 생성자

<시간/>

주제에 대해 자세히 알아보기 전에 관련된 모든 용어를 살펴보겠습니다.

복사 생성자 전달된 개체의 정확한 복사본인 개체를 만드는 데 사용되는 특수한 유형의 생성자입니다.

가상 기능 상위 클래스에서 선언되고 상위 클래스를 상속하는 하위 클래스에서 재정의(재정의)되는 멤버 함수입니다.

가상 복사 생성자를 사용하면 프로그래머는 개체의 정확한 데이터 유형을 몰라도 개체를 만들 수 있습니다.

C++ 프로그래밍 언어에서 복사 생성자는 다른 개체에서 복사한 개체를 만드는 데 사용됩니다. 그러나 프로그램이 생성된 개체의 유형에 대해 런타임에 결정하도록 하려는 경우, 즉 해당 개체 유형은 컴파일 시간이 아니라 런타임에 정의되고 특정 조건에 대해 사용자가 제공한 일부 입력을 기반으로 합니다. 이 상황에서 우리는 이 일을 하기 위해 몇 가지 특별한 능력을 가진 복사 생성자가 필요합니다. 이렇게 하기 위해 실시간으로 객체 복제를 제공하는 가상 복사 생성자가 선언됩니다.

예를 들어 프로그램을 사용하여 알아낼 영역인 인물이 있다고 가정해 보겠습니다. 그러나 객체의 유형 업은 사각형 또는 원이 될 수 있는 실시간으로 정의됩니다. 따라서 우리는 사용자가 입력한 유형에 따라 객체를 복사하는 가상 복사 생성자를 사용할 것입니다.

가상 생성자가 제대로 작동하려면 기본 클래스에 대해 정의된 두 가지 메서드가 있습니다. 그들은 -

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