原型模式之C++实现

时间:2014-06-24 14:43:23   收藏:0   阅读:218

 

 

#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;
}

 

原型模式之C++实现,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!