0

I am looking to make this more efficient, how would I Re-write the Enemy class to use inheritance and virtual functions? Including any new child classes.

class Enemy
{
public:
    int type; // 0 = Dragon, 1 = Robot
    int health; // 0 = dead, 100 = full
    string name;
    Enemy();
    Enemy(int t, int h, string n);
    int getDamage(); // How much damage this enemy does
};
Enemy::Enemy() : type(0), health(100), name("")
{ }
Enemy::Enemy(int t, int h, string n) :
    type(t), health(h), name(n)
{ }
int Enemy::getDamage() {
    int damage = 0;
    if (type == 0) {
        damage = 10; // Dragon does 10
        // 10% change of extra damage
        if (rand() % 10 == 0)
            damage += 10;
    }
    else if (type == 1) {
        // Sometimes robot glitches and does no damage
        if (rand() % 5 == 0)
            damage = 0;
        else
            damage = 3; // Robot does 3
    }
    return damage;
}

This calculates how much total damage the band will dish out.

int calculateDamage(vector<Enemy*> bandOfEnemies)
{
    int damage = 0;
    for (int i = 0; i < bandOfEnemies.size(); i++)
    {
        damage += bandOfEnemies[i]->getDamage();
    }
    return damage;
}
2
  • Yes inheritance, virtual functions and polymorphism in general seems like a good alternative. Commented Apr 30, 2020 at 1:12
  • Before deciding on interface - you should first figure out what you need. If you have a single Enemy type then there is no need for anything. If you want variety of Enemy classes than figure out which methods might differ and make then virtual. Commented Apr 30, 2020 at 1:48

1 Answer 1

1

That's a good start, but with inheritance, you don't need to be so specific. For example, in the enemy class you have an attribute type. If you want to use inheritance, you don't need to specify the type, because the derived class would be the type.

As for your function getDamage(), you can leave it blank and turn it into a virtual function. Putting all of this together, your code should look something like this:

class Enemy
{
public:
    int health; // 0 = dead, 100 = full
    string name;

    Enemy();
    Enemy(int t, int h, std::string n);

    virtual int getDamage() = 0; // pure virtual function
};

Enemy::Enemy()
    : type(0), health(100), name("") {}

Enemy::Enemy(int t, int h, std::string n)
    : type(t), health(h), name(n) {}


// class 'Dragon' inherits from class 'Enemy'
class Dragon : public Enemy
{
public:
    Dragon() {}

    int getDamage()
    {
        // dragon's damage
    }
};

Notice how if you want to create another enemy, you just inherit from the Enemy class. And this way, you can store your characters in an array like this:

vector<Enemy> enemies = {
    Dragon(),
    Dragon(),
    Robot()
};
Sign up to request clarification or add additional context in comments.

1 Comment

That vector you show will lead to object slicing and exceptions when calling the abstract base function. For polymorphism to work in C++ you need to have pointers or references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.