How much is too much Polymorphism?

How many classes should be off of a class before it's considered bad?

For example:

1
2
3
4
class Object;
class Consumable : Object;
Consumable minorHealthPotion;
Consumable majorHealthPotion;


OR

1
2
3
4
5
class Object;
class Consumable : public Object;
class HealthPotion : Consumable;
HealthPotion small;
HealthPotion medium;


Or are either of those wrong? I just want the gist of how much/little I should use polymorphism. Thanks!
There's isn't really a single answer. It all depends on what exactly it is that you're trying to model. In your example, Consumable might be a redundant class if only HealthPotion derives from it -- now and forever -- or it might not be if you're accounting for future additions.

If space efficiency is a requirement, you should take it easy on polymorphic classes (that is, classes with virtual function members), though.
Also, by the KISS principle, something simple is better than something complex if they both accomplish the same thing. Thus, needlessly deep inheritance trees are best avoided.
Last edited on
Gotcha, thanks.

And how do you spilt this up file wise?

I usually had follow the one class one file rule saying until on most things.. Is that smart for things like this? (Object, Consumable, HealthPotion).

To be honest I'd do Object by itself, consumable by itself, and then have classes off of that (health, mana, speed, food, etc) in one file.

What's your thoughts?
As a general rule, compilation time grows linearly with the number of files with a constant factor of 1 with the number of .cpp files. Additionally, every header is compiled once for every .cpp that includes it. The worst case is when you have many .cpps that all include many headers.
If your patience is in as short supply as mine, you'll want to cluster related classes in files and make use of precompiled headers, if your compiler supports them.
Topic archived. No new replies allowed.