Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm in trouble with inheritance and polymorphism in C++
    primarykey
    data
    text
    <p>I'm new to C++, and I'm trying to use inheritance but I have an error. It says </p> <blockquote> <p>Error 6 error LNK1120: 2 unresolved externals L:\2011-08\C++\Assignment\Drug Management\Debug\Drug Management.exe 1 1 Drug Management</p> </blockquote> <p>There're three classes which are Animal, Sheep, and Cattle. What I've done so far is:</p> <pre><code>//Animal.h #ifndef ANI_H #define ANI_H #include &lt;string&gt; #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 &lt;string&gt; 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-&gt;getDaysDifference() &lt; 90 || c1-&gt;getCategory() == "Meat"){ c1-&gt;dose = 0; return c1-&gt;dose; } else if(c1-&gt;getCategory() == "Dairy"){ if (c1-&gt;weight &lt; 250 || c1-&gt;dose &gt; 200){ c1-&gt;dose = 0; } else{ c1-&gt;dose = c1-&gt;weight * 0.013 + 46; } return c1-&gt;dose; } else if(c1-&gt;getCategory() == "Breeding"){ if (c1-&gt;weight &lt; 250 || c1-&gt;dose &gt; 250){ c1-&gt;dose = 0; } else{ c1-&gt;dose = c1-&gt;weight * 0.021 + 81; } return c1-&gt;dose; } else { cout &lt;&lt; "It is not valid category" &lt;&lt; endl; } } </code></pre> <p>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? </p> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload