我希望能够使用我的 hero 类对象从角色调用公共函数 getAttack() 并仅为该对象生成一个特定值。我怎样才能做到这一点?我尝试使用 setAttack,它可以工作,但是当我调用 getAttack 时没有显示任何内容。
//main
int main()
{
Characters h;//Created using normal constructor
h.setAttack(3);
h.getAttack();//i want this to lets say be 3
Hero Me;
Me.setAttack(5);
Me.getAttack();//and this like 5 or something
Hero::Hero(1,2,3,4);//Created using overloaded constructor
Monsters m;
Monsters::Monsters(5,6,7,8);
cin.sync();
cin.get();
return 0;
}
//Hero.h
class Hero:
public Characters
{
public:
Hero();
Hero(int, int, int, int);
~Hero(void);
}
//Hero.cpp
int Herolevel;
int HeroHp;
int HeroStrength;
int HeroAttack;
int HeroDefense;
Hero::Hero()
{
cout << "HOLA! Hero Created using normal constructor\n";
}
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
{
cout << "Hero created using Overloaded function!\n";
HeroHp = newHp;
cout << "Hp is: "<< HeroHp << endl;
Herolevel = newLevel;
cout << "level is: " << Herolevel << endl;
HeroAttack = newAttack;
cout << "Attack is: " << HeroAttack << endl;
HeroDefense = newDef;
cout << "Defense is: " << HeroDefense << endl;
}
Hero::~Hero(void)
{
cout << "Hero destroyed!\n";
}
//Monsters.h
class Monsters:
public Characters //Hero
{
public:
Monsters(void);
Monsters(int, int, int, int);
Monsters(int);
~Monsters(void);
};
//Monsters.cpp
int Monsterlevel;
int MonsterHp;
int MonsterStrength;
int MonsterAttack;
int MonsterDefense;
Monsters::Monsters(void)
{
cout <<"Monster Created";
}
Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
{
cout << "Monster created using Overloaded function!\n";
MonsterHp = newHp;
cout << "Hp is: "<< MonsterHp << endl;
Monsterlevel = newLevel;
cout << "level is: " << Monsterlevel << endl;
MonsterAttack = newAttack;
cout << "Attack is: " << MonsterAttack << endl;
MonsterDefense = newDef;
cout << "Defense is: " << MonsterDefense << endl;
}
Monsters::~Monsters(void)
{
cout << "\nMonster Destroyed";
}
//Characters.h
class Characters
{
private:
int level;
int Hp;
int Strength;
int Attack;
int Defense;
public:
Characters(void);
Characters(int, int, int, int);
~Characters(void);
int getAttack();
int getDefense();
int getStrength();
int getHp();
int getLevel();
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setLevel(int);
};
Characters::Characters(void)
{
cout << "\nCharacter has been created!\n";
}
Characters::~Characters(void)
{
cout << "Character has been destroyed!\n";
}
void Characters::setHp(int damage)//get Character left over hp
{
Hp -= damage;
}
void Characters::setAttack(int att)
{
Attack = att;
cout << "your attack is: " << Attack;
}
int Characters::getAttack()
{
return Attack;
}
int Characters::getDefense()
{
return Defense;
}
int Characters::getStrength()
{
return Strength;
}
int Characters::getHp()
{
return Hp;
}
int Characters::getLevel()
{
return level;
}