备忘录模式(C++)

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

class info
{
public:
    info():pc(0){}
    ~info(){}
    void input(string name,int age,int salary)
    {
        _name.push_back(name);
        _age.push_back(age);
        _salary.push_back(salary);
        pc++;
    }

    void show()
    {
        for (int i=0;i<pc;i++)
        {
            cout<<"name  :"<<_name[i]<<setw(10);
            cout<<"age   :"<<_age[i]<<setw(10);
            cout<<"salary:"<<_salary[i]<<endl;
        }
    }

    info* operator=(info* p)
    {
        info* temp;
        temp->_name=p->_name;
        temp->_age=p->_age;
        temp->_salary=p->_salary;
        temp->pc=p->pc;
        return temp;
    }

private:
    int pc;
    vector<string> _name;
    vector<int> _age;
    vector<int> _salary;

};

class Memorandum
{
public:
    Memorandum(){}
    ~Memorandum(){}

    void Save(info p)
    {
        _wh.push_back(p);
    }

    info Load()
    {
        vector<info>::reverse_iterator rit;
        rit=_wh.rbegin();
        return *rit;
    }

private:
    vector<info> _wh;
};

int main()
{
    Memorandum *p_wh=new Memorandum;
    info p_info;
    p_info.input("zhangsan",20,4000);
    p_wh->Save(p_info);
    p_info.show();
    cout<<endl;

    p_info.input("lisi",22,5000);
    p_info.input("wangwu",23,6000);
    p_info.show();

    cout<<endl;
    p_info=p_wh->Load();
    p_info.show();

    delete p_wh;
    system("pause");
    return 0;
}
posted @ 2012-06-28 20:49  Dsp Tian  阅读(585)  评论(0编辑  收藏  举报