开发者

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

I'm new to C++, and I'm trying to use inheritance but I have an error. It says

Error 6 error LNK1120: 2 unresolved externals L:\2011-08\C++\Assignment\Drug Management\Debug\Drug Management.exe 1 1 Drug Management

There're three classes which are Animal, Sheep, and Cattle. What I've done so far is:

//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 + 8开发者_JS百科1;
        }
        return c1->dose;
    }
    else
    {
        cout << "It is not valid category" << endl;
    }
}

Sheep class is pretty much same as Cattle. I'm not sure if I'm on the right track. What I'm trying to do is inherit (or polymorphism) calcDose() in Cattle.cpp taking from Animal.h. Can you give me a piece of advice?

Cheers


You declared but did not define a default constructor and a destructor for Animal, and for Cattle (and presumably the others as well). Define them, or don't declare them (you probably do need a destructor though, even if it does nothing).

Another problem is your calcDose method. One way to deal with that is to make it "pure virtual" in Animal: use virtual double calcDose() = 0;

One final problem: Don't use use namespace std;. Don't ever, ever, ever use it in a header file.


Without seeing all of your program's code, or without you specifying which symbols the linker couldn't find, it's hard to say exactly which symbols it's complaining about, but Animal is missing implementation for 3 of its member functions, and Cattle is missing implementation for 2 of its member functions.


You've declared calcDose inside your Animal class, but you haven't defined it. If you intended for that just to be implemented in the derived classes, then you should make it be pure virtual in your Animal class like this:

virtual double calcDose() = 0;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜