The functions in the Cow class don't match the type of the pure virtual functions.
For example:
bool IsMammal(void) const
is not the same as
bool IsMammal(void)
Try adding the const qualifiers to the functions in Animal.h
Thanks for helping
I am having tough time to learn pointer, virtual functions.
is this correct?
I am not sure that i wrote it right
1 2
//Animal**animal[n_animals];
Animal**animal = new Animal*[n_animals];
I really don't know what to write on animal class?
please help thanks
Error: 'Animal::~Animal' : cannot define a compiler-generated special member function (must be declared in the class first)
Question: The Animal class constructor has one string parameter: name. The constructor makes sure that the name is a non-empty string or it crashes the program. Alternatively to crashing the program, the constructor can throw an exception.
Any help
is this correct?
please help me to finish my code? It would be big step to learn C++ for me.
1 2 3 4 5 6 7 8 9 10 11 12
if(animal[secret_animal_index]->IsMammal())
cout << "I am a mammal" << endl;
else
cout << "I am not a mammal" << endl;
if(animal[secret_animal_index]->IsMeatEater())
cout << "I am a meat eater" << endl;
else
cout << "I am not a meat eater" << endl;
if(animal[secret_animal_index]->HasFourLegs())
cout << "I have four legs" << endl;
else
cout << "I do not have four legs" << endl;
Error: 'Animal::~Animal' : cannot define a compiler-generated special member function (must be declared in the class first)
Um, where is ~Animal() declared within your class? Either you remove this
1 2 3
Animal::~Animal(void)
{
}
or you declare it in your class. Hm, I can't explain it any better than the error does.
Question: The Animal class constructor has one string parameter: name. The constructor makes sure that the name is a non-empty string or it crashes the program. Alternatively to crashing the program, the constructor can throw an exception.
Now, crashing the program? How? Why?
I don't know how you want to crash your program but then better throw an exception (which is very similar to a crash)
is this correct?
it is correct as long as 'secret_animal_index' is within the array limits.
To simpilfy your array you can do the following:
1 2 3 4 5 6 7 8 9 10 11 12
Animal*animal[] =
{
new Cow,
new GreenIguana,
new KomodoDragon,
new Manatee,
new Panther,
new RattleSnake,
new Sparrow,
new SpermWhale
};
constunsigned n_animals = sizeof(animal) / sizeof(*animal);
This way the compiler counts the animals for you and you can easily add more
@coder777, thanks for replying
one more question string secret_animal_name = secret_animal->name();//line 93
error C2065: 'secret_animal' : undeclared identifier
C2227: left of '->name' must point to class/struct/union/generic type
1> type is ''unknown-type''
Actually there're 3 errors:
1. You cannot access the private member 'name'
2. 'name' is not a function hence you cannot access it with ()
You need to create a new public function like
1 2 3 4
const string &GetName() const
{
return name;
}
3. Either you use 'animal[secret_animal_index]' as before or if you want a new variable you need to declare a new one: Animal *secret_animal = animal[secret_animal_index];