请编写程序,上机调试,题目如下图,请按题目要求编写,并给出程序
#include <iostream>
#include <cstdlib>
using namespace std;
class Animal
{
private:
string nameID;
string order;
string family;
string genus;
public:
Animal(string nameID, string order, string family, string genus) : nameID(nameID), order(order), family(family), genus(family) {}
string to_string()
{
return nameID + ":" + order + "、" + family + "、" + genus;
}
virtual void who()
{
cout << this->nameID << endl;
}
virtual void sound() = 0;
};
class Sheep : public Animal //继承
{
public:
Sheep(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
void who()
{
cout << "Sheep!" << endl;
}
void sound()
{
cout << "miemie!" << endl;
}
};
class Dog : public Animal //继承
{
public:
Dog(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
void who()
{
cout << "Dog!" << endl;
}
void sound()
{
cout << "wangwang!" << endl;
}
};
class Cow : public Animal //继承
{
public:
Cow(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
void who()
{
cout << "Cow!" << endl;
}
void sound()
{
cout << "EnEn!" << endl;
}
};
class Zoo
{
private:
Animal *animals[50];
int size;
public:
Zoo() { size = 0; }
void showAnimals()
{
int sheep = 0, dog = 0, cow = 0;
for (int i = 0; i < this->size; i++)
{
Animal *temp = animals[i];
temp->who();
cout << temp->to_string() << endl;
temp->sound();
if (typeid(*temp) == typeid(Sheep))
{
sheep++;
}
if (typeid(*temp) == typeid(Dog))
{
dog++;
}
if (typeid(*temp) == typeid(Cow))
{
cow++;
}
}
cout << "羊:" << sheep << "狗:" << dog << "牛:" << cow << endl;
}
void addAnimal(Animal *p)
{
if (size < 50)
animals[size++] = p;
}
};
int main()
{
Zoo zoo;
int size = 0;
Animal *temp;
while (size++ < 30)
{
int index = rand() % 3 + 1;
switch (index)
{
case 1:
temp = new Sheep("绵羊", "偶蹄目", "牛科", "绵羊属");
break;
case 2:
temp = new Dog("狗", "食肉目", "犬科", "犬属");
break;
case 3:
temp = new Cow("牛", "偶蹄目", "牛科", "牛属");
;
break;
default:
break;
}
zoo.addAnimal(temp);
}
zoo.showAnimals();
return 0;
}
#include <iostream>
using namespace std;
#include <string>
class Animal
{
protected:
string nameID;
string order;
string family;
string genus;
public:
Animal() {}
Animal(string n,string o,string f,string g) : nameID(n),order(o),family(f),genus(g) {}
virtual void who() {cout<<nameID<<endl;}
virtual void sound() = 0;
};
class Sheep : public Animal
{
public:
Sheep(string n,string o,string f,string g) : Animal(n,o,f,g) {}
virtual void who() {cout<<"Sheep!"<<endl;}
virtual void sound() { cout<<"miemie!"<<endl;}
};
class Dog : public Animal
{
public:
Dog(string n,string o,string f,string g) : Animal(n,o,f,g) {}
virtual void who() {cout<<"Dog!"<<endl;}
virtual void sound() { cout<<"wangwang!"<<endl;}
};
class Cow : public Animal
{
public:
Cow(string n,string o,string f,string g) : Animal(n,o,f,g) {}
virtual void who() {cout<<"Cow!"<<endl;}
virtual void sound() { cout<<"moumou!"<<endl;}
};
class Zoo
{
private:
Animal *animals[50];
int size;
public:
Zoo() { size = 0;}
void showAnimals()
{
for(int i=0;i<size;i++)
{
if(animals[i] != NULL)
{
animals[i]->who();
animals[i]->sound();
}
}
}
void addAnimal(Animal *p)
{
if(size < 50)
animals[size++] = p;
}
};
int main()
{
Zoo zoo;
Sheep *pS = new Sheep("Tom","偶蹄目","牛科","绵羊属");
Dog *pD = new Dog("Jone","食肉目","犬科","犬属");
Cow *pC = new Cow("Jack","偶蹄目","牛科","牛属");
zoo.addAnimal(pS);
zoo.addAnimal(pD);
zoo.addAnimal(pC);
zoo.showAnimals();
return 0;
}
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
class Animal {
public:
enum Order {
Artiodactyla, // 偶蹄目
Perissodactyla, // 奇蹄目
Carnivora, // 食肉目
Primates // 灵长目
};
enum Family {
Bovidae, // 牛科
Canidae, // 犬科
Equidae, // 马科
Cercopithecidae // 猴科
};
enum Genus {
Ovis, // 绵羊属
Canis, // 犬属
Bos, // 牛属
Equus, // 马属
Rhinopithecus // 猴属(仰鼻猴属)
};
Animal(int nameID, Order order, Family family, Genus genus)
: _nameID(nameID), _order(order), _family(family), _genus(genus) {}
int nameID() const { return _nameID; }
Order order() const { return _order; }
Family family() const { return _family; }
Genus genus() const { return _genus; }
std::string toString() const {
std::ostringstream ss;
ss << "nameID: " << _nameID << '\n'
<< "Order: " << toString(_order) << '\n'
<< "Family: " << toString(_family) << '\n'
<< "Genus: " << toString(_genus) << '\n';
return ss.str();
}
virtual std::string who() const {
std::ostringstream ss;
ss << "Animal #" << _nameID;
return ss.str();
}
virtual std::string sound() const = 0;
static std::string toString(Order order) {
switch (order) {
case Artiodactyla:
return "Artiodactyla";
case Perissodactyla:
return "Perissodactyla";
case Carnivora:
return "Carnivora";
case Primates:
return "Primates";
default:
throw std::invalid_argument("invalid order");
}
}
static std::string toString(Family family) {
switch (family) {
case Bovidae:
return "Bovidae";
case Canidae:
return "Canidae";
case Equidae:
return "Equidae";
case Cercopithecidae:
return "Cercopithecidae";
default:
throw std::invalid_argument("invalid family");
}
}
static std::string toString(Genus genus) {
switch (genus) {
case Ovis:
return "Ovis";
case Canis:
return "Canis";
case Bos:
return "Bos";
case Equus:
return "Equus";
case Rhinopithecus:
return "Rhinopithecus";
default:
throw std::invalid_argument("invalid genus");
}
}
private:
int _nameID;
Order _order;
Family _family;
Genus _genus;
};
class Sheep : public Animal {
public:
Sheep(int nameID) : Animal(nameID, Artiodactyla, Bovidae, Ovis) {}
virtual std::string who() const {
std::ostringstream ss;
ss << "Sheep #" << nameID();
return ss.str();
}
virtual std::string sound() const { return "miemie"; }
};
class Dog : public Animal {
public:
Dog(int nameID) : Animal(nameID, Carnivora, Canidae, Canis) {}
virtual std::string who() const {
std::ostringstream ss;
ss << "Dog #" << nameID();
return ss.str();
}
virtual std::string sound() const { return "wangwang"; }
};
class Cow : public Animal {
public:
Cow(int nameID) : Animal(nameID, Artiodactyla, Bovidae, Bos) {}
virtual std::string who() const {
std::ostringstream ss;
ss << "Cow #" << nameID();
return ss.str();
}
virtual std::string sound() const { return "mowmow"; }
};
class Zoo {
public:
static const int MAX_SIZE = 50;
Zoo() : _size(0) {}
~Zoo() {
for (int i = 0; i < _size; i++)
delete _animals[i];
}
void addAnimal(Animal *animal) {
if (_size >= MAX_SIZE)
throw std::runtime_error("zoo is full");
_animals[_size++] = animal;
}
void showAnimals() const {
for (int i = 0; i < _size; i++) {
std::cout << "Who: " << _animals[i]->who() << '\n'
<< "Sound: " << _animals[i]->sound() << '\n'
<< _animals[i]->toString() << '\n';
}
}
void showStatistics() const {
int count[4] = {0};
for (int i = 0; i < _size; i++)
count[_animals[i]->family()]++;
std::cout << "Number of animals of the same family:\n";
for (int i = 0; i < 4; i++)
std::cout << Animal::toString(static_cast<Animal::Family>(i)) << ": "
<< count[i] << '\n';
}
private:
Animal *_animals[MAX_SIZE];
int _size;
};
int main() {
srand(time(NULL));
Zoo zoo;
for (int i = 0; i < 10; i++) {
switch (rand() % 3) {
case 0:
zoo.addAnimal(new Sheep(i));
break;
case 1:
zoo.addAnimal(new Dog(i));
break;
case 2:
zoo.addAnimal(new Cow(i));
break;
}
}
zoo.showAnimals();
zoo.showStatistics();
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
Who: Cow #0
Sound: mowmow
nameID: 0
Order: Artiodactyla
Family: Bovidae
Genus: Bos
Who: Sheep #1
Sound: miemie
nameID: 1
Order: Artiodactyla
Family: Bovidae
Genus: Ovis
Who: Sheep #2
Sound: miemie
nameID: 2
Order: Artiodactyla
Family: Bovidae
Genus: Ovis
Who: Sheep #3
Sound: miemie
nameID: 3
Order: Artiodactyla
Family: Bovidae
Genus: Ovis
Who: Sheep #4
Sound: miemie
nameID: 4
Order: Artiodactyla
Family: Bovidae
Genus: Ovis
Who: Dog #5
Sound: wangwang
nameID: 5
Order: Carnivora
Family: Canidae
Genus: Canis
Who: Dog #6
Sound: wangwang
nameID: 6
Order: Carnivora
Family: Canidae
Genus: Canis
Who: Dog #7
Sound: wangwang
nameID: 7
Order: Carnivora
Family: Canidae
Genus: Canis
Who: Dog #8
Sound: wangwang
nameID: 8
Order: Carnivora
Family: Canidae
Genus: Canis
Who: Sheep #9
Sound: miemie
nameID: 9
Order: Artiodactyla
Family: Bovidae
Genus: Ovis
Number of animals of the same family:
Bovidae: 6
Canidae: 4
Equidae: 0
Cercopithecidae: 0