Question about Creating an array of pointers

I want to create an array of pointers which are animal types, but I'm not sure if I did it correctly. Most likely, I didn't. Another question is that I want to return the animal type instead of Animal[rand] in line 38-40. What can I do ?

Thank You!
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
#include<iostream>
#include "Animal.h"
#include "randomizer.h"
using namespace std;
#include "Cow.h"
#include "Green_Iguana.h"
#include "Komodo_Dragon.h"

int main()
{
	Cow Cow;
	Green_Iguana Green_Iguana;
	Komodo_Dragon Komodo_Dragon;
	
	Animal* Animal[2];
	Animal[0]= &Cow;
	Animal[1]= &Green_Iguana;
	Animal[2]= &Komodo_Dragon;
	

	Randomizer r;
	int rand=r.nextInt()%3;

	cout<<Animal[rand]->Name()<<endl;

	

	system("pause");
	return 0;
}
Arrays are indexed from 0 to n-1 (number of elements), so what you need is

1
2
3
4
5
 
Animal *Animal[3]; 
Animal[0]= &Cow;
Animal[1]= &Green_Iguana;
Animal[2]= &Komodo_Dragon;
I got what you mean, but I don't get how I can use them.
I try "cout<<Animal[0]->Name()<<endl;" But it didn't return me the name "Cow"...
And I didn't get the part that use the selected animal’s characteristic methods to display its characteristics.

Here is what I wrote for the class cow and class animal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef COW_H
#define COW_H

#include "animal.h"

class Cow :	public Animal
{
public:
	Cow(void);

	bool HasFourLegs(void) const;
	bool IsMammal(void) const;
	bool IsMeatEater(void) const;
};
#endif; 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Cow.h"

Cow::Cow(void)
	:Animal(string("COW"))
{
}
bool Cow::HasFourLegs(void) const
{
	return true;
}
bool Cow::IsMammal(void) const
{
	return true;
}
bool Cow::IsMeatEater(void) const
{
	return false;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ANIMAL_H
#define ANIMAL_H

#include <iostream>
#include <exception>
#include "globalconstants.h"
using namespace std;
class Animal
{
public:
	Animal(void);
	~Animal(void);
	string Name(void)const;
	Animal(string name);
private:
	string _name;
};

#endif 


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
#include "Animal.h"


Animal::Animal(void)
{
}


Animal::~Animal(void)
{
}

string Animal::Name(void)const
{
	return _name;
}

Animal::Animal(string name)
{
	try
	{
	if (name==empty_string)
	 throw ("Is Empty");
	}
	catch (string is_empty)
	{
		cout << "An exception occurred. Exception. " << is_empty << endl;
	}

}
Last edited on
Although Animal::Animal(string name) takes a string as an argument, you're not actually assigning it to the data member _name. Thus, when you call Animal[0]->Name(), the string it returns is empty.
As for the second problem - if you want to use a base class pointer to access the overriden versions of methods in derived classes, you need to declare those methods to be virtual in the base class.
What should I do in order to sign the name to the animal?
Add _name = name; to the constructor?
Thanks! After I make all characteristic methods virtual, what should I do in order to make something like Animal[0].IsMammal=true I want to access the derived base class by the array like Animal[0].IsMammal instead of Cow.IsMammal.
You should make the "IsMammal" and other properties, a property of Animal, and not a property of Cow. Remember to make it public, or to give IsMammal and SetMammal functions.
I think the problem is that you're trying to write Object Oriented code without understanding Object Oriented Design. We all have to start somewhere, so don't feel bad about it.

Those IsX mthods shouldn't be methods, they should be interfaces, mixins or flavours.
http://en.wikipedia.org/wiki/Mixin
http://en.wikipedia.org/wiki/Flavors_(programming_language)
Can I point out that lines 11 - 13 in main are errors - you cannot have a variable name being the same as a class name. Did those errors not show up as compile errors?
Those aren't errors, just confusing code:

1
2
3
4
5
6
7
8
class Cow {};

int main()
{
    Cow Cow;
    class Cow AnotherCow = Cow;
    class Cow IThinkItsTheSameOne;
}

demo: http://ideone.com/eCY0Z1
Last edited on
Those aren't errors.


Damn!

ThingsLearnt++;

Maybe I should downgrade my comment a lot, and say that it is bad practice / confusing.

Having the object name being the same could lead newbies into directly referring to private variables.

I name my classes with a leading C, as in CCow, CHorse, CDog - then I can have objects Cow, Horse, Dog if I wanted, without too much confusion - although I would probably have named the objects something else.



Thank You a lot ! I finished this part.
I have two more questions.
The 1st question is that I want the player start the game by pressing enter and end the game by entering "no", but I fail! The game doesn't start by just pressing enter...what should I do to fix it ?
1
2
3
4
5
6
7
8
9
10
bool playthegame=true;
	cout<<"You wnat to play the game ?    )";
	char choice;
	cin>>choice;
	if (choice=="No"||choice=="No"||choice=="NO"||choice=="nO"||choice=="n"||choice=="N")
		playthegame=false;
	else if (choice=="a")
		playthegame=true;
	while (playthegame==true)
{....}
Topic archived. No new replies allowed.