1. All is ok. When you override(overload) non-virtual function in derived classes it hides the same function in base class.
2. You haven't any virtual function so you haven't virtual table and compiler doesn't know anything about functions from base class.
3. Add word "virtual" to the class Animal then in main() func create derived classes in heap
int main(int argc, char* argv[])
{
Animal* animal = new Animal();
Animal* wolf = new Wolf();
Animal* fish = new Fish();
Animal* goldfish = new GoldFish();
animal->eat();
wolf->eat();
fish->eat();
goldfish->eat();
return 0;
}
int main(int argc, char* argv[])
{
Animal* animal = new Animal();
Animal* wolf = new Wolf();
Animal* fish = new Fish();
Animal* goldfish = new GoldFish();
animal->eat();
wolf->eat();
fish->eat();
goldfish->eat();
return 0;
}
To copy to clipboard, switch view to plain text mode
Output should be
I eat like a generic animal.
I eat like a wolf!
I eat like a fish!
I eat like a goldfish!
Bookmarks