#include <iostream>
using namespace std;
class Hero;
class Weapon{
int atk;
public:
Weapon(int atk = 2):atk(atk){}
int getak(){return atk;}
void addatk(int atk){
atk+=atk;
}
virtual void getWeapon(){}
};
class knife:public Weapon{
int spd;
public:
knife(int atk = 0,int spd = 0):Weapon(atk),spd(spd){}
virtual void getWeapon(){
addatk(getak());
spd+=spd;
}
};
class axe:public Weapon{
int hp;
public:
axe(int atk=0 ,int hp = 0):Weapon(atk),hp(hp){}
virtual void getWeapon(){
addatk(getak());
hp+=hp;
}
};
class sword:public Weapon{
int def;
public:
sword(int atk = 0 ,int def = 0):Weapon(atk),def(def){}
virtual void getWeapon(){
addatk(getak());
def+=def;
}
};
class Hero{
int atk;
int def;
int spd;
int hp;
public:
Hero(int atk =0 ,int def = 0 ,int spd=0 , int hp = 0):atk(atk) , spd(spd) , hp(hp) , def(def){}
void setatk(int atk){this->atk = atk;}
void setdef(int def){this->def = def;}
void setspd(int spd){this->spd = spd;}
void sethp(int hp){this->hp = hp;}
int getatk(){return atk;}
int getdef(){return def;}
int getspd(){return spd;}
int gethp(){return hp;}
void equipWeapon(Weapon* *arr){
for(int i= 0 ; i<2 ; i++){
arr[i]->getWeapon();
}
}
};
int main(){
knife A(5,20);
axe B(1,30);
sword C(2,40);
Hero R;
Weapon*arr[3]={&A , &B , &C };
R.equipWeapon(arr);
cout << R.getatk() <<R.getdef() << R.getspd()<<R.gethp() <<endl;
}
正解————————————————————————————
#include <iostream>
using namespace std;
class Weapon;
class Hero{
int atk;
int def;
int spd;
int hp;
public:
Hero(int atk =1,int def = 1 ,int spd=0 , int hp = 0):atk(atk) , spd(spd) , hp(hp) , def(def){}
void setatk(int atk){this->atk = atk;}
void setdef(int def){this->def = def;}
void setspd(int spd){this->spd = spd;}
void sethp(int hp){this->hp = hp;}
int getatk(){return atk;}
int getdef(){return def;}
int getspd(){return spd;}
int gethp(){return hp;}
void equipWeapon(Weapon* w );
void show(){
cout << "atk"<<atk <<endl;
cout << "def"<<def <<endl;
cout << "spd"<<spd <<endl;
cout << "h p"<<hp <<endl;
cout << "-------" <<endl;
}
};
class Weapon{
int atk;
public:
Weapon(int atk = 2):atk(atk){}
int getAtk(){return atk;}
void setatk(int atk){atk+=atk; }
virtual void addProperty(Hero& hero){}
};
class knife:public Weapon{
int spd;
public:
knife(int atk = 1,int spd = 1):Weapon(atk),spd(spd){}
void setSpd(int spd){this->spd = spd;}
int getspd(){return spd;}
virtual void addProperty(Hero& hero){
int atk = hero.getatk() + this->getAtk();
int spd = hero.getspd()+this->spd;
hero.setatk(atk);
hero.setspd(spd);
}
};
class axe:public Weapon{
int hp;
public:
axe(int atk=0 ,int hp = 0):Weapon(atk),hp(hp){}
void setHp (int hp){this->hp = hp;}
int getHp(){return hp;}
virtual void addProperty(Hero& hero){
int atk = hero.getatk() + this->getAtk();
int hp = hero.gethp()+this->hp;
hero.setatk(atk);
hero.sethp(hp);
}
};
class sword:public Weapon{
int def;
public:
sword(int atk = 0 ,int def = 0):Weapon(atk),def(def){}
void setDef (int def){this->def = def;}
int getdef(){return def;}
virtual void addProperty(Hero& hero){
int atk = hero.getatk() + this->getAtk();
int def = hero.getdef()+this->def;
hero.setatk(atk);
hero.setdef(def);
}
};
void Hero::equipWeapon(Weapon* w){
w->addProperty(*this);
}
int main(){
knife A;
axe B;
sword C;
Hero h1 , h2 , h3;
h1.equipWeapon(&A);
h2.equipWeapon(&B);
h3.equipWeapon(&C);
h1.show();
h2.show();
h3.show();
}