I have a class Animal
, which has a subclass Dog
, which has a subclass BigDog
. The class Animal
has a protected int legs
. I am unable to access legs
in the BigDog
class functions or constructor.
class Animal {
protected:
int legs = 0;
Color eyeColor = Color(0,0,0);
public:
virtual string getSound() const = 0;
};
class Dog : Animal {
public:
string getSound() const override {
string sound = "";
int legI = legs;
while (legI-- > 0) {
sound+=" *step* ";
}
sound+="BARK";
return sound;
}
Dog() : Animal() {
legs = 4;
eyeColor = Color(200,128,0);
}
};
class BigDog : Dog {
public:
//use the initializer of dog
BigDog() : Dog() {
legs = 4;
}
string getSound() const override {
string sound = "";
int legI = legs;
while (legI-- > 0) {
sound+=" *step* ";
}
sound+="BOOF BOOF";
return sound;
}
};
This code gives "error: ‘int Animal::legs’ is protected" when setting or reading legs from BigDog