小菜第一版代码
//第九章原型模式
//V1版本
#include<string>
#include<iostream>
using namespace std;
class Resume {
private:
string name;
string sex;
string age;
string timeArea;
string company;
public:
Resume(string name) {
this->name = name;
}
//设置个人信息
void SetPersonalInfo(string sex, string age) {
this->sex = sex;
this->age = age;
}
//设置工作经历
void SetWorkExperience(string timeArea, string company) {
this->timeArea = timeArea;
this->company = company;
}
void Display() {
cout << name << "," << sex << "," << age << endl;
cout << "工作经历" << timeArea << "," << company << endl;
}
};
//客户端代码
int main() {
Resume a{ "大鸟" };
a.SetPersonalInfo("男","29");
a.SetWorkExperience("1998-2000", "xx公司");
Resume b{ "大鸟" };
b.SetPersonalInfo("男", "29");
b.SetWorkExperience("1998-2000", "xx公司");
Resume c{ "大鸟" };
c.SetPersonalInfo("男", "29");
c.SetWorkExperience("1998-2000", "xx公司");
a.Display();
b.Display();
c.Display();
return 0;
}
小菜第二版代码
//第九章原型模式
//V2版本
#include<string>
#include<iostream>
using namespace std;
class Resume {
private:
string name;
string sex;
string age;
string timeArea;
string company;
public:
Resume(Resume& re) = default; //V1.5显式声明合成复制构造函数
Resume(Resume&& re) = default; //V2 显式声明合成移动构造函数
Resume(string name) {
this->name = name;
}
//设置个人信息
void SetPersonalInfo(string sex, string age) {
this->sex = sex;
this->age = age;
}
//设置工作经历
void SetWorkExperience(string timeArea, string company) {
this->timeArea = timeArea;
this->company = company;
}
void Display() {
cout << name << "," << sex << "," << age << endl;
cout << "工作经历" << timeArea << "," << company << endl;
}
//V2 拷贝本身
Resume Clone() {
return Resume(*this);
}
};
//客户端代码
int main() {
Resume a{ "大鸟" };
a.SetPersonalInfo("男", "29");
a.SetWorkExperience("1998-2000", "xx公司");
Resume b{ a.Clone() };
b.SetWorkExperience("1998-2006", "yy公司");
Resume c{ a.Clone() };
c.SetPersonalInfo("男", "24");
a.Display();
b.Display();
c.Display();
return 0;
}
小菜第三版代码
//第九章原型模式
//V3版本
#include<string>
#include<iostream>
#include<utility>
using namespace std;
class WorkExperience {
public:
string workDate;
string company;
};
class Resume {
private:
string name;
string sex;
string age;
shared_ptr<WorkExperience> work;//V3
public:
Resume(Resume& re) = default; //V1.5显式声明合成复制构造函数
Resume(Resume&& re) = default; //V2 显式声明合成移动构造函数
Resume(string name) {
work.reset(new WorkExperience); //V3
this->name = name;
}
//设置个人信息
void SetPersonalInfo(string sex, string age) {
this->sex = sex;
this->age = age;
}
//设置工作经历
void SetWorkExperience(string timeArea, string company) {
work->workDate = timeArea;
work->company = company;
}
void Display() {
cout << name << "," << sex << "," << age << endl;
cout << "工作经历" << work->workDate << "," << work->company << endl;//V3
}
//V2 V3 拷贝本身
Resume Clone() {
Resume re(*this);
re.work.reset(new WorkExperience{ work->workDate,work->company });//V3
return re;
}
};
//客户端代码
int main() {
Resume a{ "大鸟" };
a.SetPersonalInfo("男", "29");
a.SetWorkExperience("1998-2000", "xx公司");
Resume b{ a.Clone() };
b.SetWorkExperience("1998-2006", "yy公司");
Resume c{ a.Clone() };
c.SetPersonalInfo("男", "24");
a.Display();
b.Display();
c.Display();
return 0;
}