Given the following classes:
class animal {
public:
virtual void noise() { };
};
class cat: public animal{
public:
void noise() {
cout << "mew" << endl;
}
};
Why does this work (prints the correct noise):
animal *animals[2];
animals[0] = new dog;
animals[0]->noise();
But this doesn't:
animal animals[2];
dog dog1;
animals[0] = dog1;
animals[0].noise();
Is there a way to make it work without using pointers? The software wants me to add more text but I think I have already made my question very clear so this is just random text...