Can someone explain if the following approach is correct for many layers of inheritance? I'm getting a bit confused...
say I have an enum class defined in a namespace that is imported and being used in all these files:
{
RED = 1,
BLUE = 2,
GREEN = 3
};
say this is the parent file:
parent.hpp
class Parent {
public:
Parent (const std::string &name = "", Color color = Color::RED)
: a_name(name)
, a_color(color) {};
Color getcolor() const { return a_color; }
protected:
std::string a_name;
Color a_color;
}
Then I have a child class that inherits the parameters and fields of parent, but has its own new parameter.
child.hpp
class Child : public Parent
{
public:
Child(std::string name, Color color, int number = 0);
protected:
int number;
}
child.cpp
Child::Child (std::string name, Color color, int number)
: parent(name, color)
, a_number(number) {};
and then grandchildren who inherit from child
Grandchild.hpp
class Grandchild : public Child
{
public:
Grandchild(Std::string name, Color color, int number, int age = 0);
private:
int a_age;
}
Grandchild.cpp
Grandchild::Grandchild (Std::string name, Color color = Color::RED, int number, int age)
: Child (name, color, number)
, a_age(age)
Let's say I want to define a color for each grandchild, would I put it inside the constructor like above? Or would I do something like this:
Grandchild::Grandchild (Std::string name, int number, int age)
: Child (name, Color::RED, number)
, a_age(age)