I've got this code I need to link together by creating the implementation (.cpp) file for.
I've been given main.cpp and a header file. I need help on my Zoo.cpp file, specifically now on the "addAnimaltoZoo" function.
Here's what I've got so far...
main.cpp:
#include <iostream>
using namespace std;
#include "Zoo.h"
int main()
{
Zoo z;
Horse * prince = new Horse(1550, "Prince");
Cow * mollie = new Cow(1220, "Mollie");
Snake * sly = new Snake(5, "Sly");
Cow * milkey = new Cow(1110, "Milkey");
z.addAnimaltoZoo(prince);
z.addAnimaltoZoo(mollie);
z.addAnimaltoZoo(sly);
z.addAnimaltoZoo(milkey);
cout << "The total weight for the zoo is "
<< z.getTotalZooWeight() << " pounds." << endl;
z.makeAllNoisesInZoo();
return 0;
}
Zoo.h:
// ***************** zoo.h *************************
enum animal_type {t_Horse, t_Cow, t_Snake};
class Animal
{
double weight;
animal_type type;
string name;
public:
Animal(double w, animal_type t, char * n):
weight(w), type(t), name(n)
{
}
void makeNoise()
{
cout << "This should never be called for any animal"
<< endl;
}
const char *getName()
{
return name.c_str();
}
double getWeight()
{
return weight;
}
animal_type getAnimalType()
{
return type;
}
};
class Horse : public Animal
{
public:
Horse (double weight, char * name);
void makeNoise(); // Horses should say Whinney
};
class Snake : public Animal
{
public:
Snake (double weight, char * name);
void makeNoise(); // Snakes should say Hiss
};
class Cow : public Animal
{
public:
Cow (double weight, char * name);
void makeNoise(); // Cows should say Mooo
};
const int ZOO_SIZE=10;
class Zoo
{
Animal * animals[ZOO_SIZE];
int animalCount;
public:
Zoo(): animalCount(0)
{
}
// The following method adds the specified animal to the Zoo
void addAnimaltoZoo(Animal *a);
// The following calls the makeNoise routines for all of the
// animals in the zoo
void makeAllNoisesInZoo();
// The following returns the total weight of the Zoo
double getTotalZooWeight();
};
And my attempt so far at the Zoo.cpp:
#include <iostream>
using namespace std;
#include "Zoo.h"
Horse::Horse (double weight, char * name) :
Animal (weight, t_Horse, name)
{}
//Horses should say Whinney
void Horse::makeNoise()
{
cout << "Whinney";
}
Snake::Snake (double weight, char * name) :
Animal (weight, t_Snake, name)
{}
// Snakes should say Hiss
void Snake::makeNoise()
{
cout << "Hiss";
}
Cow::Cow (double weight, char * name) :
Animal (weight, t_Cow, name)
{}
// Cows should say Mooo
void Cow::makeNoise()
{
cout << "Mooo";
}
// The following method adds the specified animal to the Zoo
void Zoo::addAnimaltoZoo(Animal *a)
{
if (animalCount < ZOO_SIZE)
{
Animal *b = new Animal(a);
animals[animalCount++] = b;
}
}
// The following calls the makeNoise routines for all of the
// animals in the zoo
void Zoo::makeAllNoisesInZoo()
{
}
// The following returns the total weight of the Zoo
double Zoo::getTotalZooWeight()
{
return 0;
}
Any help please!
I have to go back and redo it all with Polymorphism after I figure this part out.