I have some classes here:
class weapons
{
protected:
int damage, armor, cost;
};
class sword : public weapons
{
public:
// Initialization for the sword class.
void initialize()
{
}
};
class shield : public weapons
{
};
I started working on these and I do not remember how to set the damage, armor, and cost for each inherited class. How do I do this? What is a quick way (does not have to be easy)?
The proper way of setting up variables in a class is through the constructor of their class. Derived classes should use initializer list to set variables in their base classes.
Alternatively, if the subclass controls the values in the base (i.e. the user does not pass
damage,armor, orcostyou can do this:The compiler will optimize this code to inline things properly, so you should not worry about the performance suffering from adding an extra layer of constructors.