I am having a problem with classes Please help

closed account (96URX9L8)
I really don't know how to do multiple classes.

I am trying to write animal game which Am I meat eater or Am I mammal or Am I have four legs.

there are 8 animals
when compiler hit
1
2
3
4
if(animal[secret_animal_index]->IsMammal())
		cout << "I am a mammal" << endl;
	else 
		cout << "I am not a mammal" << endl;

result should be what animal is it and does that animal mammal or meat eater or have four legs?

Here is my code:
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
92
93
94
95
#include <iostream>
#include <string>

#include <cassert>//added
#include "Animal.h"
#include "stringManipulators.h"
#include "cow.h"
#include "greeniguana.h"
#include "komododragon.h"
#include "manatee.h"
#include "panther.h"
#include "rattleSnake.h"
#include "sparrow.h"
#include "spermwhale.h"

#include "random.h"

using namespace std;

bool AskYesNoQuestion(const string& prompt);
void playAnimalRecognizer(Animal*animal[], unsigned n_animals, unsigned secret_animal_index);

int main()
{
	Random<int> random_generator;
	const unsigned n_animals = 8;
	Animal **animal = new Animal*[n_animals];
        
        /*
        fill in missing code
        */

	string prompt = "Do you want to play animal recognizer? ";
	while(AskYesNoQuestion(prompt))
	{
		int secret_animal = random_generator.next(0, n_animals - 1);

		playAnimalRecognizer(animal, n_animals, secret_animal);

		prompt = "Do you want to play again? ";
	}

	system("pause");
	return 0;
}
bool AskYesNoQuestion(const string& prompt)
{
	cout << prompt;
	string answer;
	getline(cin, answer, '\n');
	answer = StringManipulators::trim(answer);
	answer = StringManipulators::toUpper(answer);
	return answer == StringManipulators::empty_string || answer[0] == 'Y';
}
void playAnimalRecognizer(Animal*animal[], unsigned n_animals, unsigned secret_animal_index)
{
	assert(0 <= secret_animal_index && secret_animal_index < n_animals);

	if(animal[secret_animal_index]->IsMammal()) // having problem with it
		cout << "I am a mammal" << endl;
	else 
		cout << "I am not a mammal" << endl;
	if(animal[secret_animal_index]->IsMeatEater()) //having problem with it
		cout << "I am a meat eater" << endl;
	else
		cout << "I am not a meat eater" << endl;
	if(animal[secret_animal_index]->HasFourLegs()) // having problem with it
		cout << "I have four legs" << endl;
	else
		cout << "I do not have four legs" << endl;

	cout << "Which animal do you think I am?" << endl;
	cout << "Am I a Cow?" << endl;
	cout << "Am I a Green Iguana?" << endl;
	cout << "Am I a Komodo Dragon?" << endl;
	cout << "Am I a Manatee?" << endl;
	cout << "Am I a Panther?" << endl;
	cout << "Am I a Rattle Snake?" << endl;
	cout << "Am I a Sparrow?" << endl;
	cout << "Am I a Sperm Whale?" << endl;	
	cout << "You think the animal's name is ";

	string your_animal_name;
	getline(cin, your_animal_name, '\n');
	your_animal_name = StringManipulators::trim(your_animal_name);
	string secret_animal_name = secret_animal->name();
	if(your_animal_name == secret_animal_name)
		cout <<	"You win" << endl;
	else
	{
		cout << "You lose" << endl;
		cout << "I am not a " << your_animal_name << endl;
	}
	cout << "I am a " << secret_animal_name << endl;
}

i really don't know what to write here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Animal.h"

Animal::Animal(void)
{
}
Animal::~Animal(void)
{
}
Animal::Animal(string name)
{
	
}
bool IsMammal()
{
}
bool IsMeatEater()
{
}
bool HasFourLegs()
{
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include <string>
using namespace std;

class Animal
{
public:
	Animal(void);
	Animal(string name);
	virtual ~Animal(void);

	bool IsMammal() const;
	bool IsMeatEater() const;
	bool HasFourLegs() const;
private:
	string name;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

#include "animal.h"

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

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

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::IsMammal(void) const
{
	return true;
}
bool Cow::IsMeatEater(void) const
{
	return false;
}
bool Cow::HasFourLegs(void) const
{
	return true;
}

Thanks for help
We appreciate you writing out what it should do, but you seem to be missing what it currently does do.
Animal should be an abstract class.
Some Animal's methods should be virtual.
You shouldn't ignore case when including files. It works on Windows, but that's not the only OS in the world.
closed account (96URX9L8)
I need a help please
Last edited on
closed account (96URX9L8)
please help me to solve it!!!
Last edited on
What language is more native to you?
closed account (96URX9L8)
anybody gonna help me??
You can replace line 27 to

 
Animal * animal[n_animals];


to avoid unnecessary memory allocation/deallocation. Then you have to initialize the animals on the array.
closed account (96URX9L8)
hey my friend
only problem i can not do is initialize the animals on the array
please help
1
2
3
animal[0] = new /* the animal you want */;
animal[1] = new /* another animal you want */;
animal[2] = new /* you get it */;

But that only works if you make Animal functions virtual. Otherwise, the functions called will be those defined in Animal, not in any of its subclasses.
closed account (96URX9L8)
thank you for replying :)

you mean this
animal[0] = new "Cow";
animal[1] = new "Green Iguana";
animal[2] = new "Komodo Dragon";
animal[3] = new "Manatee";
animal[4] = new "Panther";
animal[5] = new "Rattle Snake";
animal[6] = new "Sparrow?";
animal[7] = new "Sperm Whale";

is it correct?
Actually, you use the class name:

1
2
3
animal[0] = new Cow;
animal[1] = new GreenIguana;
// ... 

P.S.: But you could make a function that converts a string to an Animal *

1
2
3
4
5
6
7
8
Animal * parseAnimal(const string & str)
{
     if(str == "Cow")
          return new Cow;
     else if(str == "Green Iguana")
          return new GreenIguana;
     // ...
}
closed account (96URX9L8)
thank you for replying and helping
I have to do this way because random_generator generates random number then animal[n_animals]

animal[0] = new Cow;
animal[1] = new GreenIguana;
animal[2] = new KomodoDragon;
animal[3] = new Manatee;
animal[4] = new Panther;
animal[5] = new RattleSnake;
animal[6] = new Sparrow;
animal[7] = new SpermWhale;

where should i put them? into Animal or into main
Where you put "fill in missing code".
closed account (96URX9L8)
thank you
I am almost done
so now how to create a pure virtual base class Animal------> to animals
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::IsMammal(void) const
{
	return true;
}
bool Cow::IsMeatEater(void) const
{
	return false;
}
bool Cow::HasFourLegs(void) const
{
	return true;
}

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

Animal::Animal(void)
{
}
Animal::~Animal(void)
{
}
Animal::Animal(string name)
{
	
}
bool IsMammal()
{
}
bool IsMeatEater()
{
}
bool HasFourLegs()
{
}
Animal methods have to be virtual (and abstract).

1
2
3
4
5
6
7
8
9
10
class Animal
{
	public:
		Animal();
		Animal(string name);

		virtual bool IsMammal() = 0;
		virtual bool IsMeatEater() = 0;
		virtual bool HasFourLegs() = 0;
};
closed account (96URX9L8)
Now it gives me error
animal[0] = new Cow;<-class Cow error object of abstract class type "Cow" is not allowed:


Animal(string("Cow")) it makes me more confusing
Last edited on
That's because you are not overwriting one or more methods declared as abstract on Animal. Make sure you used the same signature (return type, name, number of parameters, parameter types). Your compiler should point you out the ones you didn't.
Topic archived. No new replies allowed.