一切运行良好,但我的问题是我的主要功能太长了。有没有办法在主功能之外创建战斗场景?我试过这个,但是如果我把战斗场景——让我们说“Battle()”——放在头文件或其他东西中,那么我的对象会产生错误。有什么方法可以让我在主函数之外的主函数中声明的对象。也许使用指针或其他东西?
int main()
{
Hero Me(100,20,30,40);//Created using overloaded constructor
Monster m(100,16,18,20);//creates a monster object and uses overloaded constructor to initialize
cout << "\nAttacking!\n";
//I want this part to be in a function called Battle() outside of main
while ((Me.getHp() > 0) && (m.getHp() > 0))//Generates error if object not declared in main
{
cout << "\nYour hp is: " << Me.getHp() << endl;
cout << "The enemy's hp is: "<< m.getHp() << endl;
cout << "\nThe monster has attacked you!\n";
cout << "You received " << m.getAttack() << " damage;" << endl;
Me.damageTaken(m.getAttack());//Me.setHp(Me.getHp() - m.getAttack());//Me.setHp(m.getStrength());
if(Me.getHp() > 0)//Check if still alive
{
cout << "\nYour hp is now: " << Me.getHp() << endl;
//cout << "Enemy hp is: "<< m.getHp() << endl;
cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
m.damageTaken(Me.getAttack());//m.setHp(m.getHp() - Me.getAttack());//m.setHp(Me.getAttack());
if(m.getHp() > 0)//Check if still alive
{
cout << "Enemy hp is now: " << m.getHp() << endl;
cout << "\nAttacking again!\n";
}
}
}
if ((Me.getHp() > 0) && (m.getHp() <= 0))
cout <<"\nCongratulations! You killed the enemy!" << endl;
else if ((Me.getHp() <= 0) && (m.getHp() > 0))
cout << "You have died!" << endl;
cin.sync();
cin.get();
return 0;
}
如果代码如下:
//Hero.h
class Hero:
public Character
{
public:
Hero();
Hero(int, int, int, int);
~Hero();
};
//Hero.cpp
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: Character(newHp, newLevel, newAttack, newDef)
{
cout << "Hero created using Overloaded function!\n";
hp = newHp;
cout << "Hp is: "<< hp << endl;
level = newLevel;
cout << "level is: " << level << endl;
attack = newAttack;
cout << "Attack is: " << attack << endl;
defense = newDef;
cout << "Defense is: " << defense << endl;
// logging goes here
// note that you don't need HeroLevel etc. at all any more, just use level
}
Hero::~Hero()
{
cout << "Hero destroyed!\n";
}
//Monster.h
class Monster:
public Character //Hero
{
public:
Monster();
Monster(int, int, int, int); //explicit
//:Character(){};
//Monster(int);
~Monster();
};
//Monster.cpp
Monster::Monster(int newHp, int newLevel, int newAttack, int newDef)
//: hp(newHp), level(newLevel), attack(newAttack), defense(newDef)//initialize list
{
cout << "Monster created using Overloaded function!\n";
hp = newHp;
cout << "Hp is: "<< hp << endl;
level = newLevel;
cout << "level is: " << level << endl;
attack = newAttack;
cout << "Attack is: " << attack << endl;
defense = newDef;
cout << "Defense is: " << defense << endl;
}
Monster::~Monster()
{
cout << "\nMonster Destroyed";
}
//Character.h
class Character
{
protected://not private, so derived class can use it
int level;
int hp;
int attack;
int defense;
Character(); // zero everything by default
Character(int); // randomly generate everything
Character(int, int, int, int); // populate explicitly
~Character();
public:
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getHp() const { return hp; }
int getlevel() const { return level; }
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setlevel(int);
void damageTaken(int);
//Character.cpp
Character::Character() : level(0), hp(0), attack(0), defense(0) {}
Character::Character(int hit, int lvl, int att, int def)
: level(lvl), hp(hit), attack(att), defense(def){}
Character::~Character()
{
cout << "Character has been destroyed!\n";
}