Simple Inheritance Query

Hello,

I am new to C++ and haven't quite grasped structs or classes/objects yet. I'm looking to make a very simple text console game using enemies with slightly different health and attack power. I'm thinking it should use 'inheritance' of some kind but am not 100% sure I'm looking at this the right way.

PSEUDOCODE:-

EnemyClass
HP
ATK

Enemy1.EnemyClass
HP=10
ATK=1

Enemy2.EnemyClass
HP=15
ATK=2

Enemy2 attacks you for Enemy2.ATK damage.
You hit Enemy2 for 15 damage.
Enemy2.HP - 15 = 0.
Enemy2 dies.

Would I want to look into classes/objects, structs, or something different?

Any help would be greatly appreciated =)

Many thanks!
Last edited on
Use inheritance if you want to extend a class. If an enemy has additional skills or you want to change a certain behavior.

In your case Enemy1 and Enemy2 are just instances of the same class
You dont really need to use inheritance here, but you want to (may be for learning) then

declare a "power" class and then derive the Enemy class and a "GoodGuy" class from it since you need a "GoodGuy" to attack the enemy and enemy as well as the GoodGuy should have both HP and ATK.

For the values, you pass them to the constructor while creating objects.
Something like Enemy enemy1(10, 1); you example of Enemy1
Last edited on
Ahh ok =)

So if I wanted a default class and I wanted to make a difference version of that enemy (different hp/atk) it would be an object?

However if I wanted to make GoodGuy from that class it would be something more like: -

PSEUDOCODE:-

Classes
-------
PowerClass
HP
ATK

Enemy.PowerClass
LootItem=1

GoodGuy.PowerClass
BLOCK=1

Contructed
----------
Zombie Enemy (10, 1);
Skeleton Enemy (20, 2);
Hero GoodGuy (50, 15);


Zombie attacks you for Zombie.ATK damage.
You hit Zombie for Hero.ATK damage.
Zombie.HP - Hero.ATK = 0.
Zombie dies.
Yep
Ok great, I'll give it a go =)

Thanks again!
Topic archived. No new replies allowed.