#include<stdio.h>
#include<stdlib.h>
#include <string.h>
class Memento
{
public:
char name[64];
int hp;
int mp;
Memento()
{
}
Memento(char *name,int hp,int mp)
{
strcpy(this->name,name);
this->hp=hp;
this->mp=mp;
}
};
class Gamerole
{
public:
char name[64];
int hp;
int mp;
Memento* get_mem();
int recover_mem(Memento* m);
private:
Memento mem;
};
class caretaker
{
private:
Memento mem;
public:
int set_mem(Memento* m)
{
strcpy(mem.name,m->name);
mem.hp=m->hp;
mem.mp=m->mp;
};
Memento* get_mem()
{
return &mem;
};
};
Memento* Gamerole::get_mem()
{
return (new Memento(this->name,this->hp,this->mp));
};
int Gamerole::recover_mem(Memento* m)
{
strcpy(this->name,m->name);
this->hp=m->hp;
this->mp=m->mp;
};
#include"memento.h"
using namespace std;
int main()
{
Gamerole myrole;
strcpy(myrole.name,"haopeng");
myrole.hp=99;
myrole.mp=100;
//before fight
puts("before fight...");
printf("name is %s,hp is %d,mp is %d\n",myrole.name,myrole.hp,myrole.mp);
caretaker buckup;
buckup.set_mem(myrole.get_mem());
//fighting
puts("fighting...");
myrole.hp = 2;
myrole.mp = 3;
printf("name is %s,hp is %d,mp is %d\n",myrole.name,myrole.hp,myrole.mp);
//recovery
puts("recovery ...");
myrole.recover_mem(buckup.get_mem());
printf("name is %s,hp is %d,mp is %d\n",myrole.name,myrole.hp,myrole.mp);
};