I’m trying to store things into vector but there’re few problems.
These are my code.
Animal.cpp
#include "Animal.h"
#include "Cattle.h"
#include "Sheep.h"
#include <iostream>
#include <string>
using namespace std;
Animal::Animal(int newid, double newweight, int yy, int mm, int dd, double newaccDose, char newsex)
{
id = newid;
weight = newweight;
yy = yy;
mm = mm;
dd = dd;
dose = newaccDose;
sex = newsex;
}
Animal::Animal()
{
id = 0;
weight = 0;
yy = 0;
mm = 0;
dd = 0;
dose = 0;
sex = ' ';
}
Animal::~Animal(){}
double Animal::getDaysDifference(){
jdate dateOfBirth(dd,mm,yy);
jdate now;
double diff = now-dateOfBirth;
return diff;
}
void Animal::addAnimal(){
int select=0;
int id;
cout << "1. Cattle 2. Sheep" << endl;
cout << "Select a type of animal: ";
cin >> select;
if(select==1){
Cattle* c1;
cout << "Please answer following questions" << endl;
cout << "Type the animal's ID: ";
cin >> id;
c1->setID(id);
//vector<Animal> vec;
//vector<Animal>::iterator vec_ite;
}else if(select==2){
Sheep* s1;
cout << "Please answer following questions" << endl;
cout << "Type the animal's ID: ";
cin >> id;
s1->setID(id);
}
else{
cout << "Invalid number please try again" << endl;
}
}
First off, in the code above, Cattle* c1 and Sheep* s1, it doesn’t have an error.
and even if I code like s1->setID(id) it’s fine. However,
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;
}
Cattle::Cattle(){
id = 0;
weight = 0;
dose = 0;
sex = ' ';
Cattle::category = "";
}
Cattle::~Cattle(){}
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;
}
}
Animal.h
#ifndef ANI_H
#define ANI_H
#include <vector>
#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() = 0;
void addAnimal();
double getDaysDifference();
};
#endif
DrugAdmin.cpp (main)
#include <iostream>
#include "Animal.h"
#include "Cattle.h"
using namespace std;
int main(){
Animal* a1=new Animal;
delete a1;
a1->addAnimal();
}
The problem is here, when I try to define Animal* a1=new Animal; it says
delete a1;Object of abstract class type "Animal" is not allowed:
I’ve Googled and Binged here and there, and I figured out I cannot define the abstract base class itself but subclasses.
So I tried the same thing in Cattle.cpp using Cattle c1=new Cattle; but it doesn’t work either..
I’m really not sure what’s wrong with that..
Could you help me please?
Cheers
virtual double calcDose()This says “It is possible to
calcDose()for everything which is an Animal, and different Animals may do it differently…”= 0;“… but there is no ‘generic’ way to do it for Animals; each one must specify its own method”.
You may not construct an unspecified sort of Animal because, by definition, a key piece of information is missing: how to
calcDosefor it.This is all well and good. Chances are, you don’t really want to construct an unspecified Animal, for the same reason that in real life, you wouldn’t go to a farm (Zoo? Pet store?) to buy “an animal” without having any specific preconception of what kind of Animal you want.
The type
Animalexists so that you can omit the information about what kind of Animal something is when it is appropriate to do so. Creating the instance is not one of those times, in your case.Also,
addAnimalmakes absolutely no sense as a member function ofAnimal, no matter how much you might think it does. There are three fundamental problems:1) since it’s a non-static member function, you already require an
Animalto call the function. That’s kind of a catch-22.2) You don’t have anywhere to add the animal to. You need some kind of container.
3) Conceptually, making a list of Animals is not the animals’ responsibility. It’s the responsibility of whoever needs the list.
4) In general, it is good policy to have clean separation between the code that asks the user for information, and the code which acts upon that information. Write a free (meaning, not part of a class) function that asks for the information, constructs the Animal and returns that pointer; then in the calling code, store the pointer in a container.
Except you don’t really want to use raw pointers for this anyway. You should be using some kind of smart pointer, or else putting the pointers into one of the boost
ptr_containers.