#define HUMAN_PARTY_SIZE 3
#define WIZARD_PARTY_SIZE 3
void rest_human(struct human* p)
{
p->HP = p->max_HP;
}
void rest_wizard(struct wizard* p)
{
p->HP = p->max_HP;
p->MP = p->max_MP;
}
void scene3()
{
struct human m0, m1, m2;
struct wizard w0, w1, w2;
int x[3] = {2,3,4};
struct human* human_party[HUMAN_PARTY_SIZE] = {&m0, &m1, &m2};
struct wizard* wizard_party[WIZARD_PARTY_SIZE] = {&w0, &w1, &w2};
int i;
for (i = 0; i < HUMAN_PARTY_SIZE; i++) {
rest_human(human_party[i]);
}
for (i = 0; i < WIZARD_PARTY_SIZE; i++) {
rest_wizard(wizard_party[i]);
}
}
問題文::
同じことをクラスHumanとWizardを用いて記述せよ。HumanとWizardの回復処理インタフェースを統一して、
一つのfor文で処理すること。
以下自分の回答(不備・不要なものアリ)
#include<iostream>
using namespace std;
#define HUMAN_PARTY_SIZE 3
#define WIZARD_PARTY_SIZE 3
class Chuman{
protected:
int HP;
int max_HP;
int party_size;
public:
Chuman(int hp,int mhp){set_HP(hp);set_max_HP(mhp);}
Chuman(){};
void set_max_HP(int max_number);
void set_HP(int now_number);
void set_party_size(int party);
int get_party_size();
int get_max_HP();
int get_HP();
void out();
virtual void rest(Chuman *p){p->HP = p->max_HP;};
friend ostream& operator<<(ostream& out,const Chuman &f){
return out<<"HP:"<<f.HP<<"/"<<f.max_HP;
}
};
void Chuman::set_party_size(int party){
party_size=party;
}
void Chuman::out(){
cout << "HP:" <<get_HP()<<"/"<<get_max_HP()<<"\n";
}
void Chuman::set_max_HP(int max_number){
max_HP=max_number;
}
void Chuman::set_HP(int now_number){
HP=now_number;
}
int Chuman::get_party_size(){
return party_size;
}
int Chuman::get_max_HP(){
return max_HP;
}
int Chuman::get_HP(){
return HP;
}
class Cwizard:public Chuman{
protected:
int max_MP;
int MP;
public:
Cwizard(int hp,int mhp,int mp,int mmp){set_HP(hp);set_max_HP(mhp);set_MP(mp);set_max_MP(mmp);}
Cwizard(){};
void set_max_MP(int max_mnumber);
void set_MP(int now_mnumber);
int get_max_MP();
int get_MP();
void out();
virtual void rest(Cwizard *p){p->HP = p->max_HP;p->MP=p->max_MP;};
friend ostream& operator<<(ostream& out,const Cwizard &f){
return out<<"HP:"<<f.HP<<"/"<<f.max_HP<<"\nMP:"<<f.MP<<"/"<<f.max_MP;
}
};
void Cwizard::out(){
cout << "HP:" <<get_HP()<<"/"<<get_max_HP()<<"\n"<<"MP:"<<get_MP()<<"/"<<get_max_MP()<<"\n";
}
void Cwizard::set_max_MP(int max_mnumber){
max_MP=max_mnumber;
}
void Cwizard::set_MP(int now_mnumber){
MP=now_mnumber;
}
int Cwizard::get_max_MP(){
return max_MP;
}
int Cwizard::get_MP(){
return MP;
}
void scene3()
{
Chuman* rest[2];
Chuman* human_party[HUMAN_PARTY_SIZE] = {&Chuman(5,8),&Chuman(2,9),&Chuman(7,11)};
Cwizard* wizard_party[WIZARD_PARTY_SIZE] = {&Cwizard(2,4,3,10),&Cwizard(1,5,8,12),&Cwizard(4,8,4,8)};
Chuman *a[100];
rest[0]=&human_party;
rest[1]=&wizard_party;
int i,j;
for(j=0;j<2;j++){
for(i=0;i<3;i++) {
Chuman* party[2]={human_party[i],wizard_party[i]};
rest[j]->rest(party[j]);
cout<<*party[j]<<"\n";
}
}
cout<<*wizard_party[1]<<"\n";
}
void main(){
Chuman CH;
scene3();
}