DarkVapor
DarkVapor

Reputation: 61

Using inheritance to initialize parameters in C++

I have read that it is not good to overuse inheritance in C++. I have a simple example where I would like to know if it is good or bad to use it to initialize values in the base class constructor.

Let's say I have a class Animal :

class Animal{
protected :
Animal(std::string name, std::string category);
std::string m_name;
std::string m_category;
};

Is it good to create a specific class for each animal the application is going to use for example :

class Bear : public Animal{
public :
Bear():Animal("Bear", "Mammal"){
}
};

Of course that means that if I have 100 animals, I need 100 classes, and that feels awkward to my eyes. In this example, I am using inheritance only to initialize values, there won't be any specific virtual methods involved.

Thanks

Upvotes: 1

Views: 93

Answers (4)

ProXicT
ProXicT

Reputation: 1933

Usually, it's not a good practice to have members in the base class. Rather than that, create an interface and provide an implementation in the derived class returning the values:

class Animal {
protected:
    virtual std::string getName() const;
    virtual std::string getCategory() const;
};

class Bear : public Animal{
public:
    virtual std::string getName() const override {
        return std::string("Bear");
    }

    virtual std::string getCategory() const override {
        return std::string("Mamal");
    }
};

Upvotes: 1

TheUndeadFish
TheUndeadFish

Reputation: 8171

Broadly speaking, the choice of whether to create derived classes or to just have data members on one class becomes a question of whether more classes actually help with the situation you're trying model/solve in the software.

For instance, an application that cares about how vehicles travel might have different classes for Car, Boat, Airplane, Helicopter, etc. On the other hand an application that only cares about the existence of vehicles (say it's for tracking what a business owns) might only need a single class for Vehicle in general.

As one rule of thumb, ask yourself if the derived classes are actually going to behave differently in any way, as far as what the rest of your program is concerned with. If yes, then derived classes may be what you want. If no, then probably not.

In your specific case, is there going to be any real difference between

Bear bear;

and

Animal bear("Bear", "Mammal");

If not, then a derived class of each type of Animal sounds excessive (100 derived classes sounds excessive in general) and you probably want to do something else depending on how you want to store the data. Perhaps making a list or map of Animal instances.

Upvotes: 3

VoidChips
VoidChips

Reputation: 337

Making a class for each animal seems excessive. I did a school work before where I used an Animal class and I made the derived class into Predator and Prey. So, make the derived ones be specific types of animal, like Mammal, Fishes, etc. For your example, you can just use the single Animal class and call that Animal bear.

Upvotes: 0

Nathanael Demacon
Nathanael Demacon

Reputation: 1217

Inheritance is useful when you have different cases of implementation but still have the same working base behind. In your case if the only difference between all your animals is the name and category and that there's no specific implementations then I personally think this is not useful.

However what could be improved in your case is the need of 2 arguments that define the same thing (a Bear is always a mammal for example). Maybe a map could fix this issue in your case?

Upvotes: 0

Related Questions