简体   繁体   English

派生类C ++

[英]Derived Classes C++

I'm working on a text based game for fun and I'm struggling with inheritance from a base class. 我正在开发一款基于文本的游戏,很有趣,而且我在为基类的继承而苦苦挣扎。 I have my base class Being which holds all the stats for whatever character I create. 我有我的基类Being ,其中包含我创建的任何角色的所有统计信息。 I then have a class Combat that I want to take all the stats from Being (Or from a Being and pass it into combat?) and do all the combat stuff. 然后,我有一个“ Combat类,我想从“ Being所有统计数据(或从“存在”中获取并传递到战斗中?)并进行所有战斗工作。 But I'm not quite understanding inheritance, or at least how to declare the functions in Main to get it to work. 但是我不太了解继承,或者至少不了解如何在Main声明函数以使其起作用。 If I keep the attack function in Being , I can just write this line in main: 如果我将Attack函数保留在Being ,则可以在main中编写以下行:

human.attack(monster);

But after splitting the combat into another class, I'm not sure how to write that in main so it works. 但是,在将战斗划分为另一个类之后,我不确定如何在main中编写该类,使其有效。 Please & thank you for your help! 请并感谢您的帮助!

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
}

Then my derived class: 然后是我的派生类:

class Combat : public Being
{
private:

public:

    void attack(Being& target);

};

Combat.cpp: 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);
    }
}

Main: 主要:

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);
}

It doesn't seem like you are leveraging inheritance correctly here, unless the following is true: 除非满足以下条件,否则您似乎在这里未正确利用继承:

In your game, there is a distinction between beings that can attack, and beings that cant. 在您的游戏中,可以攻击的生物和不能攻击的生物是有区别的。 That being said, changing what your inheritance hierarchy could be useful. 话虽如此,更改继承层次结构可能会很有用。

Consider making the 'type' of being what you inherit as. 考虑成为继承人的“类型”。

Ex: 例如:

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
};

This wouldn't be something you would want to have an inheritance, combat would not be a type of being. 这不是您想要继承的东西,战斗不是存在的一种。 For example an apple is a kind of fruit, so a fruit base class and apple derived class would be logically correct. 例如,苹果是一种水果,因此,水果基类和苹果派生类在逻辑上是正确的。 I would recommend creating an Enemy class and a Hero class, deriving monsters such as zombies and ninjas off of enemy. 我建议创建一个敌人类和一个英雄类,从敌人身上衍生出诸如僵尸和忍者之类的怪物。

#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);


}

Why don't you declare human to be of type Combat instead? 您为什么不宣布humanCombat类型?

Combat human("", "FirstofFurry", 2, 1, 2, 50, 50, 20, 30, 7);

It still has access to all of the public methods/data of Being , but can also use the Combat -only attack method. 它仍然可以访问Being所有公共方法/数据,但是也可以使用“仅Combat attack方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM