Hello Virtual Function Animal

Hello Guys
I am trying to write C++ codes but i got some error.
Last edited on
What were the errors? What message? What line of code caused it?
there is on Line 31
'Animal' certainly has a/some pure virtual function(s) which is/are not overwritten
can you help me? i don't know what to write?
closed account (D80DSL3A)
coder777 probably nailed it.

Show us the Animal class so we can see which pure virtual function(s) that you haven't defined in the Cow class.
Thanks for replying
Here you go:
Animal header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <iostream>
#include <string>
using namespace std;

class Animal
{
public:
	Animal();
	Animal(const string Name);
	
	virtual bool IsMammal() = 0;
	virtual bool IsMeatEater() = 0;
	virtual bool HasFourLegs() = 0;
private:
	string name;
};

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

Animal::Animal(void)
{
}
Animal::~Animal(void)
{
}
Animal::Animal(string name)
{
}
bool IsMammal()
{
}
bool IsMeatEater()
{
}
bool HasFourLegs()
{
}
Last edited on
closed account (D80DSL3A)
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
1
2
3
virtual bool IsMammal() const = 0;
virtual bool IsMeatEater() const = 0;
virtual bool HasFourLegs() const = 0;


Why do you have definitions for these functions in Animal.cpp ?
1
2
3
4
5
6
7
8
9
bool IsMammal()
{
}
bool IsMeatEater()
{
}
bool HasFourLegs()
{
}

I don't think those belong there.

Lastly, why not assign the name in the Animal constructor?
You have:
1
2
3
Animal::Animal(string name)
{
}

Why not:
1
2
3
4
Animal::Animal(string Name)
{
    name = Name;
}

?
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.

Last edited on
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;
Last edited on
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
};
const unsigned 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''

i don't know where to declare and point
Last edited on
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];
Topic archived. No new replies allowed.