Skamah One
Skamah One

Reputation: 2456

Should I inherit a subclass if some of the instances don't use an attribute of base class?

The question might be a bit unclear, so I'll just provide the code:

class HitPoints {
    int m_current;
    int m_maximum;
    int m_regeneration;
};

So basically current means the hit points you currently have, maximum is the maximum hit points you can have, and regeneration is how much hp you get every 10 seconds.

Now the thing is; not each creature in my game uses the regeneration to regenerate their hp, some just can't regenerate hp over time.

Should I just set regeneration to 0 for those creatures, or should I inherit a new class:

class HitPoints {
    int m_current;
    int m_maximum;
};

class RegeneratingHitPoints : public HitPoints {
    int m_regeneration;
};

I think it doesn't matter that much for this small example, but using the knowledge I've learned, I should inherit a new class.

My question is, does it really matter for one attribute? And to make it more generic question, how strict is it to create new classes? Can I sometimes just "bend" a little and use an old class, even if I should inherit a new one?

Upvotes: 1

Views: 111

Answers (2)

Skamah One
Skamah One

Reputation: 2456

Well obviously I should inherit a new class, since there should be no attributes that aren't used in the base class.

Upvotes: 0

SubSevn
SubSevn

Reputation: 1028

Unless the storage of that extra int would become an issue (if you had a lot of creatures in memory at once), I wouldn't create an entirely new class just to get rid of it. In my opinion, if you don't regenerate hit points, your hit point regeneration is zero.

Upvotes: 5

Related Questions