I have four .cpp files, Animal, Cattle, Sheep, DrugAdmin
.
Animal
is parent class of Cattle
, and it has calcDose()
, which calculates the amount of dose. DrugAdmin
is main function.
The thing is, I want to use calcDose()
function differently (Cattle, Sheep) and no calcDose()
function is needed for Animal
class. However, every time I try to use calcDose()
, it automatically calls function in Animal
class even when I want to use under Cattle
class. This is the code I have done so far. (I've cut it down)
Animal.cpp
#include "Animal.h"
#include <string>
using namespace std;
Animal::Animal(int newid, double newweight, int yy, int mm, int dd, char newsex, vector<Treatment> treatArray)
{
id = newid;
weight = newweight;
yy = yy;
mm = mm;
dd = dd;
accDose = 0;
sex = newsex;
}
double Animal::calcDose(){
return 0;
}
Cattle.cpp
#include "Cattle.h"
using namespace std;
Cattle::Cattle(int newid, double newweight, int yy, int mm, int dd,
char newsex, vector<Treatment> newtreatArray, string newcategory)
: Animal(newid, newweight, yy,mm,dd, newsex, newtreatArray)
{
id = newid;
weight = newweight;
accDose = 0;
sex = newsex;
Cattle::category = newcategory;
}
Cattle::~Cattle(){}
double Cattle::calcDose(){
if(getDaysDifference() < 90 || getCategory() == "Meat"){
accDose = 0;
return accDose;
}
else if(getCategory() == "Dairy"){
if (weight < 250 || accDose > 200){
accDose = 0;
}
else{
accDose = weight * 0.013 + 46;
}
return accDose;
}
else if(getCategory() == "Breeding"){
if (weight < 250 || accDose > 250){
accDose = 0;
}
else{
accDose = weight * 0.021 + 81;
}
return accDose;
}
else
{
//cout << "It is not valid category" << endl;
}
}
Sheep class is pretty much same but the contents of calcDose()
DrugAdmin.cpp
#include "DrugAdmin.h"
using namespace std;
vector<Animal*> vec_Animal;
void addAnimal(){
int select=0;
int id;
double weight;
int yy;
int mm;
int dd;
char sex;
string category;
vector<Treatment> treatArray;
//user inputs all the values (i've cut it down)
Animal* c1 = new Cattle(id,weight,yy,mm,dd,sex,treatArray,category);
vec_Animal.push_back(c1);
}
void administerDose(int id) //Main Problem
{
vector<Animal*>::iterator ite_Animal = vec_Animal.begin();
for(ite_Animal; ite_Animal != vec_Animal.end(); ++ite_Animal)
cout<<"\nVector contains:"<< (*ite_Animal)->calcDose();
}
I'm sorry for the long and messed up question. Final question is, Cattle has extra data member which is category
but the system doesn't recognise this as well. It recognises as if it is Animal
object. Can I have a piece of advice please?
Cheers