ก่อนที่จะขุดลึกลงไปในหัวข้อ เรามาทำความเข้าใจคำศัพท์ที่เกี่ยวข้องกันก่อน
ตัวสร้างการคัดลอก เป็นคอนสตรัคเตอร์ชนิดพิเศษที่ใช้สร้างอ็อบเจกต์ที่เป็นสำเนาของอ็อบเจกต์ที่ส่งผ่าน
ฟังก์ชันเสมือน เป็นฟังก์ชันสมาชิกที่ประกาศในคลาสหลักและถูกกำหนดใหม่ ( แทนที่) ในคลาสย่อยที่สืบทอดคลาสหลัก
ด้วยการใช้คอนสตรัคเตอร์สำเนาเสมือน โปรแกรมเมอร์จะสามารถสร้างอ็อบเจ็กต์ได้โดยไม่ต้องรู้ประเภทข้อมูลที่แน่นอนของอ็อบเจกต์
ในภาษาการเขียนโปรแกรม 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