I'm designing a simple tracking program which builds a vector of pointers to objects which represent animals in a zoo.
I declared my vector as: vector<Animal*> zooAnimals
, Animal*
being a pointer to the Animal
class.
The problem lies in the inheritance with child classes from Animal
. For example, Animal
is the parent for Mammal
, which is in turn the parent for Bat
. But when I create Bat*
and add it to the vector, it doesn't pick up on all the other fields it was supposed to have inherited. It just picks up on the fields from Animal
(the tracking number and animal name), but nothing from Mammal
(type
, subType
, and nursingStatus
).
The way I have it set up is that my program calls a function createNewZooAnimal()
which creates and returns an Animal*
(pointer to object of type animal), by (theoretically) calling the specific constructor for the animal's sub-type and returning a pointer to the newly created object (i.e., Crocodile*
or Bat*
, etc). However, these newly created pointers aren't inheriting from either Mammal
/Oviparous
or from the specific constructor for their own class. My header files do not seem to have any errors, nor the cpp file implementing them. The system happily creates the specifc pointers I need, but does not inherit anything beyond the most basic class Animal
.
Animal* createNewZooAnimal(int trackingNum, string name, string type, string subType, int numEggs, int nursing) {
//using a simple if-else if structure here, we cover each sub-type, calling the constructor for each to create the pointer for each object
if (subType == "Crocodile") {
Crocodile* croc1 = new Crocodile(numEggs); //calls constructor and creates a pointer to the new object of class Crocodile
croc1->trackingNum = trackingNum;
croc1->name = name;
return croc1; // returns pointer to get included in the vector
}
else if (subType == "Goose") {
Goose* goose1 = new Goose(numEggs); //calls the constructor for Goose, passing in numEggs, and assigns a pointer to the location of the new object
goose1->trackingNum = trackingNum;
goose1->name = name;
return goose1; //returns pointer to get included in the vector
}
else if (subType == "Pelican") { //constructs new pelican object and returns its pointer
Pelican* pelican1 = new Pelican(numEggs);
pelican1->trackingNum = trackingNum;
pelican1->name = name;
return pelican1;
}
else if (subType == "Bat") { //constructs new bat object and returns its pointer
Bat* bat1 = new Bat(nursing);
bat1->trackingNum = trackingNum;
bat1->name = name;
return bat1;
}
else if (subType == "Whale") { //constructs a new whale object and returns its pointer
Whale* whale1 = new Whale(nursing);
whale1->trackingNum = trackingNum;
whale1->name = name;
return whale1;
}
else if (subType == "SeaLion") { //constructs a new sea lion object and returns it pointer
SeaLion* seaLion1 = new SeaLion(nursing);
seaLion1->trackingNum = trackingNum;
seaLion1->name = name;
return seaLion1;
}
The way I have setup things is that the user will enter the info about the animal (name, tracking number, type (mammal/oviparous), sub-type (bat, whale, etc)...) and then pass that to the pointer creator createNewZooAnimal
to call the specific constructor for the animal's sub-type. The function then returns a pointer to this object which is then pushed onto the vector:
cout << "Enter 'Y' or 'y' to save this animal to the system, or 'N' or 'n' to cancel: " << endl;
cin >> addAnimalConfirm;
if (addAnimalConfirm == 'Y' || addAnimalConfirm == 'y') { // if user confirms addition of animal, parameters are passed
// to pointer return function, where a new animal object is created
// and we receive the pointer to it. This is then pushed onto our vector
// of pointers
Animal* temp = createNewZooAnimal(trackingNum, name, type, subType, numEggs, nursing);
zooAnimals.push_back(temp);
cout << "Animal added to Wildlife Zoo's system successfully." << endl;
}
This is the Animal.h code:
class Animal{
public:
std::string name;
int trackingNum;
Animal() {
trackingNum = 0;
name = " ";
}
};
And the Mammal.h, for example:
class Mammal : public Animal {
public:
Mammal();
Mammal(int nursingIndicator);
void displayAnimalData();
std::string type;
std::string subType;
int getNursing();
void setNursing(int numberEggs);
private:
int nursing;
};
And Bat.h:
class Bat : public Mammal {
public:
Bat();
Bat(int nursingIndicator);
};