首页 > 代码库 > 原型模式之C++实现
原型模式之C++实现
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class WorkExperience
{
};
class ProtoType
{
public:
ProtoType() {}
virtual ~ProtoType() {}
virtual ProtoType* Clone() = 0;
virtual void Display() = 0;
};
class Resume : public ProtoType
{
private:
string name;
int age;
string sex;
public:
Resume(string name, int age, string sex)
{
this->name = name;
this->age = age;
this->sex = sex;
}
Resume(const Resume& resume)
{
this = new Resume;
}
void ShowInfo()
{
cout << this->name << "\t";
cout << this->age << "\t";
cout << this->sex << "\t";
cout << endl;
}
ProtoType* Clone()
{
return new Resume(*this);
}
void Display()
{
ShowInfo();
}
};
int main()
{
ProtoType *pResume1 = new Resume("ÕÅÈý", 30, "ÄÐ");
pResume1->Display();
ProtoType *pResume2 = pResume1->Clone();
pResume2->Display();
return 0;
}
#include <string>
#include <iostream>
using namespace std;
class WorkExperience
{
};
class ProtoType
{
public:
ProtoType() {}
virtual ~ProtoType() {}
virtual ProtoType* Clone() = 0;
virtual void Display() = 0;
};
class Resume : public ProtoType
{
private:
string name;
int age;
string sex;
public:
Resume(string name, int age, string sex)
{
this->name = name;
this->age = age;
this->sex = sex;
}
Resume(const Resume& resume)
{
this = new Resume;
}
void ShowInfo()
{
cout << this->name << "\t";
cout << this->age << "\t";
cout << this->sex << "\t";
cout << endl;
}
ProtoType* Clone()
{
return new Resume(*this);
}
void Display()
{
ShowInfo();
}
};
int main()
{
ProtoType *pResume1 = new Resume("ÕÅÈý", 30, "ÄÐ");
pResume1->Display();
ProtoType *pResume2 = pResume1->Clone();
pResume2->Display();
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。