B
bsruth
I tried for an hour to find some reference to concrete information on
why this particular inheritance implementation is a bad idea, but
couldn't. So I'm sorry if this has been answered before. Here's the
scenario:
We have a base class with all virtual functions. We'll call this the
Animal class. We then make two classes Fish and Bird that both inherit
from Animal. In the program, we have a single array of Animal pointers
that will house all of the Fish and Bird objects . For the most part,
they share functions (Eat, Breathe, etc.), so you would be able to just
call AnimalArray->Eat() and it is handled polymorphically. Here is
my problem, let's say that you want to add a RuffleFeathers function to
the Bird class. Now in order to still call
AnimalArray->RuffleFeathers(), I would have to add that function
definition to the Animal class, even though not all animals will have
feathers. So I just put a vitual function declaration and a stub with
a return statement in the Animal class, so if I call
AnimalArray->RuffleFeathers() on a Fish object, it will just execute
the stub defined in Animal. To me, it just feels wrong to do it that
way.
Here's what the code would look like:
class Animal {
virtual void Eat() {return;}
virtual void RuffleFeathers() {return;}
};
class Bird : public Animal {
void Eat() {printf("Eat a mouse.");}
void RuffleFeathers {printf("Feathers ruffled.")};
};
class Fish : public Animal {
void Eat() {printf("Eat some kelp.");}
};
int main()
{
Animal* animalArray = new Animal[100];
animalArray[0] = new Fish();
animalArray[1] = new Bird();
animalArray[0]->Eat();
}
Now take this small example and extend it to 10 classes that inherit
from Animal with about 6 or so functions, like RuffleFeathers, that
have nothing to do with any of the other inherited classes. The thing
is that the implementation works, and you don't have to do a type check
or cast on each element in the single array before calling a function.
Nor do you have to keep multiple arrays of each type. But it just
seems wrong to change the base class to accomidate one of the children.
I have been under the impression that base classes should know nothing
of their children, and the sole purpose of inheritance was to extend
base classes. I just have no solid reason why this is a bad
implementation (if it even is a bad implementation). Any thoughts
would be great.
why this particular inheritance implementation is a bad idea, but
couldn't. So I'm sorry if this has been answered before. Here's the
scenario:
We have a base class with all virtual functions. We'll call this the
Animal class. We then make two classes Fish and Bird that both inherit
from Animal. In the program, we have a single array of Animal pointers
that will house all of the Fish and Bird objects . For the most part,
they share functions (Eat, Breathe, etc.), so you would be able to just
call AnimalArray->Eat() and it is handled polymorphically. Here is
my problem, let's say that you want to add a RuffleFeathers function to
the Bird class. Now in order to still call
AnimalArray->RuffleFeathers(), I would have to add that function
definition to the Animal class, even though not all animals will have
feathers. So I just put a vitual function declaration and a stub with
a return statement in the Animal class, so if I call
AnimalArray->RuffleFeathers() on a Fish object, it will just execute
the stub defined in Animal. To me, it just feels wrong to do it that
way.
Here's what the code would look like:
class Animal {
virtual void Eat() {return;}
virtual void RuffleFeathers() {return;}
};
class Bird : public Animal {
void Eat() {printf("Eat a mouse.");}
void RuffleFeathers {printf("Feathers ruffled.")};
};
class Fish : public Animal {
void Eat() {printf("Eat some kelp.");}
};
int main()
{
Animal* animalArray = new Animal[100];
animalArray[0] = new Fish();
animalArray[1] = new Bird();
animalArray[0]->Eat();
}
Now take this small example and extend it to 10 classes that inherit
from Animal with about 6 or so functions, like RuffleFeathers, that
have nothing to do with any of the other inherited classes. The thing
is that the implementation works, and you don't have to do a type check
or cast on each element in the single array before calling a function.
Nor do you have to keep multiple arrays of each type. But it just
seems wrong to change the base class to accomidate one of the children.
I have been under the impression that base classes should know nothing
of their children, and the sole purpose of inheritance was to extend
base classes. I just have no solid reason why this is a bad
implementation (if it even is a bad implementation). Any thoughts
would be great.