Polymorphism and Inheritance

Hi!

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!

Thanks
Owen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>

class Animal 
{
protected:
	const char * noise1;
	const char * noise2;
public:
	Animal(const char * a, const char * b)
		: noise1(a) , noise2(b)
	{}
	virtual void fly(void)
	{
		/*empty*/
		std::cout << noise1 << std::endl;
	}
}; // Rectify the problem in (b)
class Flyer : public Animal 
{
public:
	Flyer(const char * a, const char * b) 
		: Animal(a,b)
	{}
	void fly(void) override
	{
		std::cout << noise2 << std::endl;
	}

};
class NewMcDonald 
{
private:
Animal** _farm; // New McDonald had a farm (still has now)
const int _size; // Fixed farm size of 5

public:
	NewMcDonald() 
		: _size(5)
	{ /* TODO: Create your farm, an array of Animal* elements */
		_farm = new Animal *[_size];
	}

	~NewMcDonald() {
	/* TODO: New McDonald has no (more) farm... */
		int i = 0 ;
		for(;i<_size ; ++i)
			if(_farm[i] != NULL)
				delete _farm[i];

		delete[] _farm;
	}

	void makeSomeNoise() 
	{ /* TODO: Make sound(s) without looking out for Flyers...! */
		_farm[0]->fly();
		_farm[1]->fly();
		_farm[2]->fly();
		_farm[3]->fly();
		_farm[4]->fly();
	}
	void fillThisFarm() 
	{
	_farm[0] = new Flyer("Parrot", "squak");
	_farm[1] = new Animal("Cow", "moo");
	_farm[2] = new Flyer("Mosquito", "buzz");
	_farm[3] = new Animal("Sheep", "mehh");
	_farm[4] = new Animal("Fish", "blurp");

	}
};

int main()
{
	NewMcDonald instance;
	instance.fillThisFarm();
	instance.makeSomeNoise();
	system("pause");
	return 0;
}

/*
OUtput : 

squak
Cow
buzz
Sheep
Fish

*/


Try this , reply if you have any comments. Hope that help.
Topic archived. No new replies allowed.