C++ Class Inheritance Create several classes which are inheriting from Animal class

Description

C++ Class Inheritance Create several classes which are inheriting from Animal class

 
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
 
using std::string;
 
class Animal//  w ww . ja v  a 2 s.  c  o  m
{
private:
  string name;                                              // Name of the animal
  int weight {};                                         // Weight of the animal
 
public:
  Animal(string theName, int wt);                       // Constructor
  virtual string who() const;                              // Return string containing name and weight
  virtual string sound() const = 0;                        // Display sound of an animal
 
};
 
class Sheep: public Animal
{
public:
  Sheep(string theName, int wt) : Animal(theName, wt) {}
  string who() const override;                             // Return string containing name and weight
  string sound() const override;                           // Display sound of a sheep
};
 
class Dog: public Animal
{
public:
  Dog(string theName, int wt) : Animal(theName, wt) {}
  string who() const override;                             // Return string containing name and weight
  string sound() const override;                           // Display sound of a dog
};
 
class Cow : public Animal
{
public:
  Cow(string theName, int wt) : Animal(theName, wt) {}
  string who() const override;                             // Return string containing name and weight
  string sound() const override;                           // Return sound of a cow
};
 
// The Zoo class representing a collection of animals
class Zoo
{
private:
  std::vector<PAnimal> animals;                            // Stores smart pointers to the animals
 
public:
  Zoo() = default;                                         // Default constructor for an empty zoo
  Zoo(std::vector<PAnimal>& new_animals);                  // Constructor from a vector of animals
  void addAnimal(PAnimal animal);                          // Add an animal to the zoo
  void showAnimals();                                      // Output the animals and the sound they make
};
 
// Constructor
Animal::Animal(string theName, int wt) : name(theName), weight(wt) {}
 
// Return string describing the animal
string Animal::who() const
{
  return string("My name is ") + name + ". My weight is " + std::to_string(weight) + " lbs.";
}
 
// Return string describing the sheep
string Sheep::who() const
{
  return string("I am a sheep. ") + Animal::who();   // Call base function for common data
}
 
// Make like a sheep
string Sheep::sound() const
{
  return string("Baaaa!!");
}
 
// Return string describing the dog
string Dog::who() const
{
  return string("I am a dog. ") + Animal::who();     // Call base function for common data
}
 
// Make like a dog
string Dog::sound() const
{
  return string("Woof woof!!");
}
 
// Return string describing the cow
string Cow::who() const
{
  return string("I am a cow. ") + Animal::who();     // Call base function for common data
}
 
// Make like a cow
string Cow::sound() const
{
  return string("Mooooo!!");
}
 
// Constructor from a vector of animals
Zoo::Zoo(std::vector<PAnimal>& new_animals) : animals {new_animals} {}
 
// Add an animal to the zoo
void Zoo::addAnimal(PAnimal animal)
{
  animals.push_back(animal);
}
 
// Output the animals and the sound they make
void Zoo::showAnimals()
{
  for(auto animal : animals)
    std::cout << animal->who() << ' ' << animal->sound() << std::endl;
}
 
// 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));
}
 
class Animal;                                              // Class name declaration
 
using PAnimal = std::shared_ptr<Animal>;                   // required to compile this alias
 
 
int main()
{
  std::srand((unsigned)std::time(0));       // Seed random number generator
 
  std::vector<string> dogNames { "F", "R" , "L" , "A",   "P","S", "G" , "Q" , "W" , "E"   };
  std::vector<string> sheepNames { "B", "K", "T", "W", "C","A", "C", "E" , "S"  , "P"   };
  std::vector<string> cowNames { "D", "A", "B", "C", "D","E", "F", "G" , "H", "I" };
 
  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 a dog in pounds
  int maxSheepWt {150};                  // Maximum weight of a dog in pounds
  int minCowWt {800};                    // Minimum weight of a dog in pounds
  int maxCowWt {1500};                   // Maximum weight of a dog in pounds
 
  std::vector<PAnimal> animals;             // Stores smart pointers to animals
  int nAnimals {};                       // Number of animals to be created
  std::cout << "How many animals in the zoo? ";
  std::cin >> nAnimals;
 
  for (int i {}; i < nAnimals; ++i){
    switch(random(3))
    {
    case 0:                // Create a sheep
      animals.push_back(std::make_shared<Sheep>(sheepNames[random(sheepNames.size())], minSheepWt + random(maxSheepWt-minSheepWt+1)));
      break;
    case 1:                // Create a dog
      animals.push_back(std::make_shared<Dog>(dogNames[random(dogNames.size())], minDogWt+random(maxDogWt-minDogWt+1)));
      break;
    case 2:                // Create a cow
      animals.push_back(std::make_shared<Cow>(cowNames[random(cowNames.size())], minCowWt+random(maxCowWt-minCowWt+1)));
      break;
    default:
      std::cout << "\nInvalid animal type selection." << std::endl;
      break;
    }
  }
  Zoo theZoo(animals);
  theZoo.showAnimals();                             // Display the animals
}



PreviousNext

Related