I would like to create an Animal
interface. The Cat
class implements it. An animal can eat an another animal eat(Animal a)
. Animal.die()
will kill the animal, and Animal.isDead()
returns, if the animal is dead, or not.
If I would like to compile it, I get some errors:
templates may not be 'virtual'
invalid use of incomplete type 'class A'
expected class-name before '{' token
I've searched a lot, how to fix this errors. But none of them resolved it. I'm not a C++
expert. I only have some years of JAVA experience.
Code:
#include <iostream>
using namespace std;
//interface
class Animal {
public:
template<class A extends Animal>
virtual void eat(A* a) = 0;
virtual void die() = 0;
virtual bool isDead() = 0;
};
// Cat class
class Cat: Animal {
private:
bool dead = false;
public:
Cat();
Cat(const Cat& orig);
virtual ~Cat();
template<class A extends Animal>
void eat(A* a);
void die();
bool isDead();
};
// Implement cat
Cat::Cat() {
}
Cat::Cat(const Cat& orig) {
}
Cat::~Cat() {
}
template<class A extends Animal>
void Cat::eat(A* a) {
a->die();
}
void Cat::die() {
dead = true;
}
bool Cat::isDead() {
return dead;
}
int main(int argc, char** argv) {
Cat* cat = new Cat();
Cat* cat2 = new Cat();
cat->eat(cat2);
cout << (cat2->isDead()? "Cat2 is dead" : "Cat2 is not dead") << endl;
return 0;
}