视觉我在C++的继承和多态性方面遇到了麻烦
visual I'm in trouble with inheritance and polymorphism in C++
我是c++的新手,我试图使用继承,但我有一个错误。上面写着
L:2011-08 c++ AssignmentDrug ManagementDebugDrug Management.exe 11 Drug Management
有三个类:动物、羊和牛。到目前为止,我所做的是:
//Animal.h
#ifndef ANI_H
#define ANI_H
#include <string>
#include "Treatment.h"
#include "jdate.h"
class Animal{
protected:
int id;
double weight;
int yy;
int mm;
int dd;
double dose;
char sex;
//Treatment treatArray[];
public:
Animal();
Animal(int newid, double newweight, int yy, int mm, int dd, double newdose, char newsex);
~Animal();
virtual double calcDose();
//void addAnimal();
double getDaysDifference();
};
#endif
//Cattle.h
#ifndef CATT_H
#define CATT_H
#include "Animal.h"
#include <string>
using namespace std;
class Cattle : public Animal{
private:
string category;
public:
Cattle();
Cattle(int newid, double newweight, int yy, int mm, int dd,
double newdose, char newsex, string newcategory);
~Cattle();
virtual double calcDose();
string getCategory();
};
#endif
//Animal.cpp
#include "Animal.h"
using namespace std;
Animal::Animal(int newid, double newweight, int yy, int mm, int dd, double newaccDose, char newsex)
{
id = newid;
weight = newweight;
dose = newaccDose;
sex = newsex;
}
double Animal::getDaysDifference(){
jdate dateOfBirth(dd,mm,yy);
jdate now;
double diff = now-dateOfBirth;
return diff;
}
//Cattle.cpp
#include "Cattle.h"
Cattle::Cattle(int newid, double newweight, int yy, int mm, int dd,
double newdose, char newsex, string newcategory)
: Animal(id, weight, yy,mm,dd, dose, sex)
{
id = newid;
weight = newweight;
dose = newdose;
sex = newsex;
Cattle::category = newcategory;
}
string Cattle::getCategory(){
return category;
}
double Cattle::calcDose(){
Cattle* c1;
if(c1->getDaysDifference() < 90 || c1->getCategory() == "Meat"){
c1->dose = 0;
return c1->dose;
}
else if(c1->getCategory() == "Dairy"){
if (c1->weight < 250 || c1->dose > 200){
c1->dose = 0;
}
else{
c1->dose = c1->weight * 0.013 + 46;
}
return c1->dose;
}
else if(c1->getCategory() == "Breeding"){
if (c1->weight < 250 || c1->dose > 250){
c1->dose = 0;
}
else{
c1->dose = c1->weight * 0.021 + 81;
}
return c1->dose;
}
else
{
cout << "It is not valid category" << endl;
}
}
羊类与牛类几乎相同。我不确定我是否走对了路。我要做的是继承(或多态性)calcDose()在牛。cpp从动物。h。你能给我一个建议吗?
欢呼
您声明了但没有定义Animal和Cattle的默认构造函数和析构函数(可能还有其他类型)。定义它们,或者不声明它们(尽管你可能确实需要一个析构函数,即使它什么也不做)。
另一个问题是你的calcDose
方法。解决这个问题的一种方法是在Animal中设置为"纯虚拟":使用virtual double calcDose() = 0;
最后一个问题:不要使用use namespace std;
。永远,永远,永远不要在头文件中使用
如果没有看到程序的所有代码,或者没有指定链接器找不到哪些符号,很难确切地说出它抱怨的是哪些符号,但是Animal缺少3个成员函数的实现,而Cattle缺少2个成员函数的实现。
您已经在Animal类中声明了calcDose,但还没有定义它。如果你只打算在派生类中实现它,那么你应该在Animal类中使它是纯虚拟的,像这样:
virtual double calcDose() = 0;
相关文章:
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 如何调用指针类型的方法(禁用多态性)?
- 从基类调用函数的多态性
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?
- 视觉我在C++的继承和多态性方面遇到了麻烦