视觉我在C++的继承和多态性方面遇到了麻烦

visual I'm in trouble with inheritance and polymorphism in C++

本文关键字:方面 多态性 遇到 麻烦 继承 C++ 视觉      更新时间:2023-10-16

我是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;