我正在开发基于文本的游戏以获得乐趣,而我正在努力继承
来自基类。我有我的基类Being
,它包含所有的统计数据
我创造的任何角色。然后我有一个我想要的课程Combat
所有来自Being
的统计数据(或来自存在并将其传递给战斗?)并且这样做
所有战斗的东西。但我不太了解继承,或者至少
如何在Main
中声明函数以使其工作。如果我继续攻击
在Being
中的函数,我可以在main中写这一行:
human.attack(monster);
但是在将战斗分成另一个班级之后,我不知道该怎么写 在主要,所以它的工作原理。请&谢谢你的帮助!
class Being // Base Class
{
public:
string name, nameSpecialAttack; // Implement a special attack feature,
// that has a specific mutliplier #
// unique to each being
int attackBonus, attackMod; // was a float attackMod method for casting
// spells that double or halve a Being’s
// attack power.
int baseDamage; // base damage
int health, healthMax; // current health and max health
int mp, mpMax; // current mp and max mp
int arrows; // change to [ranged?] ammo
Being(); // Default Constructor
Being(string name, string nameSpecialAttack, int attackBonus,
int attackMod, int baseDamage, int health, int healthMax,
int mp, int mpMax, int arrows); // Constructor 2
// All my set and get functions
}
然后我的派生类:
class Combat : public Being
{
private:
public:
void attack(Being& target);
};
Combat.cpp:
void Combat::attack(Being& target)
{
//unsigned seed = time(0);
//srand(seed);
// Rand # 0-99, * damage+1, /100, * attackMod, parse to int.
int damage = (int)(attackMod*( ( (rand()%100)*(baseDamage+1) /100) + attackBonus + (rand()%2)));
target.health -=damage;
cout << name << " attacks " << target.name << " doing " << damage << " damage!" << endl;
cout << target.name << "'s health: " << target.health << endl;
// Use getHealth() instead and put this function there
if(target.health <= 0)
{
cout << endl << name << " killed " << target.name << "! You have won the game! " << endl << endl;
cout << "Terminating AMnew World, Good Bye.\n" << endl << endl;
exit(0);
}
}
主:
Being human("", "FirstofFurry", 2, 1, 2, 50, 50, 20, 30, 7); // A thing of
// class Being
Being monster("Armored Goblin", "Rawr", 2, 1, 2, 65, 59, 20, 20, 6);
int main()
{
human.attack(monster); // No longer works since attack is in
// combat class now
Sleep(3000);
cout << endl << endl;
monster.attack(human);
}
答案 0 :(得分:3)
除非以下情况属实,否则您似乎没有正确利用继承:
在你的游戏中,可以攻击的生物和无法生存的生物之间存在区别。话虽这么说,改变你的继承层次结构可能是有用的。
考虑将“继承”作为“继承”的类型。
例如:
class Being
{
public:
virtual void attack(...) = 0;
};
class Human : public Being
{
public:
virtual void attack(...); // Overrides attack in a manner that humans generally would
};
class Goblin : public Being
{
public:
virtual void attack(...); // Goblin attack style. This is actually a decent way to handle different types of attacks, as in the special attack in your above definition
};
答案 1 :(得分:3)
这不是你想要继承的东西,战斗不是一种存在。例如,苹果是一种水果,因此水果基类和苹果派生类在逻辑上是正确的。我建议创建一个敌人类和一个英雄类,从敌人那里获取诸如僵尸和忍者之类的怪物。
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class Character
{
public:
Character(){} //does nothing, just represents the heros class
};
class Enemy
{
private:
int Health; //base class variables
int WeaponID;
public:
Enemy(int hp, int wpID);
virtual void Attack(Character& Target); //if all monsters have same attack, no need for virtual
//etc...etc...
};
class Zombie:public Enemy //DERIVED CLASS, uses base class attack method
{
public:
Zombie(int hp, int wpID):
Enemy(hp,wpID)
{}
};
class Ninja: public Enemy //DERIVED CLASS, uses its own attack method
{
private:
int NumThrowingKnives;
public:
Ninja(int Hp , int ID) : Enemy(Hp,ID)
{}
void Attack(Character& Target); //different attack
//etc..etc..
};
Enemy::Enemy(int hp, int wpID)
{
Health = hp;
WeaponID = wpID;
}
void Ninja::Attack(Character& Target)
{
cout << "Ninja attack!" << endl;
}
void Enemy::Attack(Character& Target)
{
cout << "Base class attack!" << endl;
}
int main()
{
Character Bob;
Ninja Bill(50,12);
Zombie Rob(50,16);
Bill.Attack(Bob);
cout << endl;
Rob.Attack(Bob);
}
答案 2 :(得分:1)
为什么不将human
声明为Combat
类型?
Combat human("", "FirstofFurry", 2, 1, 2, 50, 50, 20, 30, 7);
它仍然可以访问Being
的所有公开方法/数据,但也可以使用Combat
- attack
方法。