Reputation: 123
I am trying to build a zoo for practicing c++ and oop. I have made 2 classes, Animal class (the base class) and Bear class (the derived class). I want to have 2 virtual functions in Animal that I will override in Bear but CLION tells me that 'Function Walk did not decleared in class Bear'.
What do I need to change?
This is the base class (Animal) header:
class Animal {
public:
Animal();
Animal(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment);
virtual void Talk() = 0;
virtual void Walk();
int CageNum;
string FoodType;
string Gender;
bool NeedTreatment;
string Name;
};
CPP:
Animal::Animal() {};
Animal::Animal(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment) :
Name(Name), CageNum(CageNum), FoodType(FoodType), Gender(Gender), NeedTreatment(NeedTreatment){};
This is the derived class (Bear) header:
#include "Animal.h"
class Bear : public Animal{
protected:
string FurColor;
public:
Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor);
};
and this is the CPP:
#include "Bear.h"
Bear::Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor) :
Animal(Name, CageNum, FoodType, Gender, NeedTreatment),FurColor(FurColor) {};
void Bear::Walk() {
cout << "Bear Moves";
}
void Animal::Talk() {
"Bear Noise";
}
Upvotes: 4
Views: 1709
Reputation: 1
If you define a function like
void Bear::Walk() {
cout << "Bear Moves";
}
its declaration must appear in the class definition:
class Bear : public Animal{
protected:
string FurColor;
public:
Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor);
void Walk() override; // <<<<<<<<<<<<<<<<<<
};
Alternatively you can omit the definition, and the base class member definition Animal::Walk()
will be used.
Also
void Animal::Talk() {
"Bear Noise";
}
is wrong (or at last doesn't do what is intended).
A pure virtual function like Talk()
from the base class, must have a declaration and definition in the Bear
class, unless the class is intentionally left abstract.
class Bear : public Animal{
protected:
string FurColor;
public:
Bear(string Name, int CageNum, string FoodType, string Gender, bool NeedTreatment,string FurColor);
void Talk() override; // <<<<<<<<<<<<<<<<<<
void Walk() override;
};
and the definition
void Bear::Talk() {
"Bear Noise";
}
Upvotes: 3