題目:定義了一個基類Animal,它包含兩個私有數據成員,一個是string成員,存儲動作的名稱(“Fido”),一個是整數成員weight,存儲了動物的重量(單位是磅)。該基類還包含一個公共的虛擬成員函數who()和一個純虛函數sound(),公共的虛擬成員函數who()返回一個string對象,該對象包含了Animal對象的名稱和重量,純虛函數sound()在派生類中應返回一個string對象,表示該動物發出的聲音。把Animal類作為一個公共基類,派生至少三個類Sheep、Dog和Cow,在每個類中實現sound()函數。
定義一個類Zoo,它至多可以在一個數組中存儲50種不同類型的動作(使用指針數組)。編寫一個main()函數函數,創建給定數量的派生類對象的隨機序列,在Zoo對象中存儲這些對象(使用指針數組)。編寫一個main()函數,創建給定數量的派生類對象的隨機序列,在Zoo對象中存儲這些對象的指針。使用Zoo對象的一個成員函數,輸出Zoo中每個動物的信息,以及每個動物發出的聲音。
參考答案:

Animal.h
// Animal.h
// Animal classes and class defining a zoo
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
using std::string;
class Animal {
public:
Animal(string theName, int wt); // Constructor
virtual string who() const; // Return string containing name and weight
virtual string sound() = 0; // Display sound of an animal
private:
string name;
int weight;
};
class Sheep: public Animal {
public:
Sheep(string theName, int wt): Animal(theName, wt) {}
string who() const; // Return string containing name and weight
string sound(); // Display sound of a sheep
};
class Dog: public Animal {
public:
Dog(string theName, int wt): Animal(theName, wt) {}
string who() const; // Return string containing name and weight
string sound(); // Display sound of a dog
};
class Cow: public Animal {
public:
Cow(string theName, int wt): Animal(theName, wt) {}
string who() const; // Return string containing name and weight
string sound(); // Return sound of a cow
};
// The Zoo class shows how you can use an enumeration
// to get a constant as part of a class definition.
// This is used to specify the dimension of the array member.
class Zoo {
public:
Zoo(): number(0) {} // Default constructor for an empty zoo
Zoo(Animal** pNewAnimal, int count); // Constructor from an array of animals
bool addAnimal(Animal* pAnimal); // And an animal to the zoo
void showAnimals(); // Output the animals and the sound they make
private:
enum { maxAnimals = 50 }; // Defines maximum number of animals
Animal* pAnimals[maxAnimals]; // Stores addresses of the animals
int number; // Number of animals in the Zoo
};
#endif

Animal.cpp
// Animal.cpp
// Implementations of the Animal class and classes derived from Animal
#include "Animal.h"
#include <string>
#include <cstdlib>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
// Constructor
Animal::Animal(string theName, int wt): name(theName), weight(wt) {}
// Return string decribing the animal
string Animal::who() const {
char wtstr[10];
return string("My name is ") + name + string(". My weight is ") + string(itoa(weight, wtstr, 10)) + string(" lbs.");
}
// Return string decribing the sheep
string Sheep::who() const {
return string("I'm a sheep. ") + Animal::who(); // Call base function for common data
}
// Make like a sheep
string Sheep::sound() {
return string("Baaaa!!");
}
// Return string decribing the dog
string Dog::who() const {
return string("I'm a dog. ") + Animal::who(); // Call base function for common data
}
// Make like a dog
string Dog::sound() {
return string("woof woof!!");
}
// Return string decribing the cow
string Cow::who() const {
return string("I'm a cow. ") + Animal::who(); // Call base function for common data
}
// Make like a cow
string Cow::sound() {
return string("Mooooo!!!");
}
// Constructor from an array of animals
Zoo::Zoo(Animal** pNewAnimals, int count) {
if (count > maxAnimals) {
cout << "\nMaximum animals allowed is " << maxAnimals
<< ". Only that number stored.";
count = maxAnimals;
}
for (int i=0; i<count; i++) {
pAnimals[i] = pNewAnimals[i];
}
number = count;
}
// Add an animal to the zoo
// Returns true if the animal is added successfully.
bool Zoo::addAnimal(Animal* pAnimal) {
if (number == maxAnimals) {
cout << "\nThe zoo is full. Animal not stored.";
return false;
}
pAnimals[number++] = pAnimal;
return true;
}
// Output the animals and the sound they make
void Zoo::showAnimals() {
for (int i=0; i<number; i++)
cout << endl << pAnimals[i]->who() << ' ' << pAnimals[i]->sound();
}

main.cpp
// main.cpp -- Exercising Zoo and Animal classes
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Animal.h"
using std::cout;
using std::endl;
using std::string;
// Function to generate a random integer 0 to count-1
int random(int count) {
return static_cast<int>((count*static_cast<long>(rand()))/(RAND_MAX+1L));
}
void main() {
srand((unsigned)time(0)); // Seed rand number generator
char* dogNames[] = { "Fido", "Rover", "Lassie", "Lambikins", "Poochy",
"Spit", "Gnasher", "Samuel", "Wellington", "Patch" };
char* sheepNames[] = { "Bozo", "Killer", "Tasty", "Woolly", "Chops",
"Blackie", "Whitey", "Eric", "Sean", "Shep" };
char* cowNames[] = { "Dolly", "Daisy", "Shakey", "Amy", "Dilly",
"Dizzy", "Eleanor", "Zippy", "Zappy", "Happy" };
int minDogWt = 1; // Minimum weight of a dog in pounds
int maxDogWt = 120; // Maximum weight of a dog in pounds
int minSheepWt = 80; // Minimum weight of sheep in pounds
int maxSheepWt = 150; // Maximum weight of sheep in pounds
int minCowWt = 800; // Minimum weight of cow in pounds
int maxCowWt = 1500; // Maximum weight of cow in pounds
Animal* pAnimals[20]; // Array to store pointers to animals
int numAnimals = sizeof pAnimals/sizeof(Animal*);
// Create random animals
for (int i=0; i<numAnimals; i++) {
switch(random(3)) {
case 0: // Create a sheep
pAnimals[i] = new Sheep(sheepNames[random(sizeof sheepNames/sizeof(char*))],
minSheepWt + random(maxSheepWt - minSheepWt + 1));
break;
case 1: // Create a dog
pAnimals[i] = new Dog(dogNames[random(sizeof dogNames/sizeof(char*))],
minSheepWt + random(maxDogWt - minDogWt + 1));
break;
case 2: // Create a cow
pAnimals[i] = new Cow(cowNames[random(sizeof cowNames/sizeof(char*))],
minCowWt + random(maxCowWt - minCowWt + 1));
break;
default:
cout << "\nInvalid animal type selection.";
break;
}
}
// Create a Zoo containing the animals
// You could also create an empty zoo and add animals one at a time
Zoo theZoo(pAnimals, numAnimals);
theZoo.showAnimals(); // Display the animals
// Release memory for the animals
// Note that the array was not created dynamically
// so we must not try to delete the array - just the individual elements
for (int j = 0; j < numAnimals; j++)
delete pAnimals[j];
cout << endl;
}