I've just started on C++ programming recently and i'm having trouble with this question.
Essentially, this is a question from one of my lectures.
<
class Animal { ... }; // Rectify the problem in (b)
class Flyer : public Animal { ... };
class NewMcDonald {
private:
Animal** _farm; // New McDonald had a farm (still has now)
const int _size; // Fixed farm size of 5
public:
NewMcDonald() { /* TODO: Create your farm, an array of Animal* elements */
}
~NewMcDonald() {
/* TODO: New McDonald has no (more) farm... */
}
void makeSomeNoise() { /* TODO: Make sound(s) without looking out for Flyers...! */
}
void fillThisFarm() {
_farm[0] = new Flyer("Parrot”, "squak");
_farm[1] = new Animal("Cow", "moo");
_farm[2] = new Flyer("Mosquito", "buzz");
((Flyer*)_farm[2])->fly();
_farm[3] = new Animal("Sheep", "mehh");
_farm[4] = new Animal("Fish", "blurp");
}
};
>
I don't quite understand why the pointer in "fillThisFarm()" can be used and what it's function is.
Additionally, to use polymorphism to output a certain noise from either of _farm[i], I will need to add the "virtual" keyword to the base class? How should i go about declaring it in main to output makeSomeNoise?
Sorry if I sound a bit confusing, cause i'm quite confused at the moment!