MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Exercises/Modules/Chapter 15/Soln15_05/Soln15_05.cpp:21–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21int main()
22{
23 const size_t num_name_options{ 10 };
24 using NameOptions = std::array<std::string_view, num_name_options>;
25
26 const NameOptions dogNames {
27 "Fido", "Rover" , "Lassie" , "Lambikins", "Poochy",
28 "Spit", "Gnasher", "Samuel" , "Wellington", "Patch"
29 };
30 const NameOptions sheepNames {
31 "Bozo", "Killer", "Tasty", "Pete", "Chops",
32 "Blackie", "Whitey", "Eric" , "Sean", "Shep"
33 };
34 const NameOptions cowNames {
35 "Dolly", "Daisy", "Shakey", "Amy", "Dilly",
36 "Dizzy", "Eleanor", "Zippy" , "Zappy", "Happy"
37 };
38
39 const unsigned minDogWt{ 1 }; // Minimum weight of a dog in pounds
40 const unsigned maxDogWt{ 120 }; // Maximum weight of a dog in pounds
41 const unsigned minSheepWt{ 80 }; // Minimum weight of a dog in pounds
42 const unsigned maxSheepWt{ 150 }; // Maximum weight of a dog in pounds
43 const unsigned minCowWt{ 800 }; // Minimum weight of a dog in pounds
44 const unsigned maxCowWt{ 1500 }; // Maximum weight of a dog in pounds
45
46 auto randomAnimalType { createUniformPseudoRandomNumberGenerator(0, 2) }; // 0, 1, or 2
47 auto randomNameIndex { createUniformPseudoRandomNumberGenerator(0, num_name_options - 1) };
48 auto randomDogWeight { createUniformPseudoRandomNumberGenerator(minDogWt, maxDogWt) };
49 auto randomSheepWeight{ createUniformPseudoRandomNumberGenerator(minSheepWt, maxSheepWt) };
50 auto randomCowWeight { createUniformPseudoRandomNumberGenerator(minCowWt, maxCowWt) };
51
52 std::vector<AnimalPtr> animals; // Stores smart pointers to animals
53 size_t numAnimals {}; // Number of animals to be created
54 std::cout << "How many animals in the zoo? ";
55 std::cin >> numAnimals;
56
57 Zoo zoo; // Create an empty Zoo
58
59 // Create random animals and add them to the Zoo
60 for (size_t i {}; i < numAnimals; ++i)
61 {
62 switch (randomAnimalType())
63 {
64 case 0: // Create a sheep
65 zoo.addAnimal(std::make_shared<Sheep>(sheepNames[randomNameIndex()], randomSheepWeight()));
66 break;
67 case 1: // Create a dog
68 zoo.addAnimal(std::make_shared<Dog>(dogNames[randomNameIndex()], randomDogWeight()));
69 break;
70 case 2: // Create a cow
71 zoo.addAnimal(std::make_shared<Cow>(cowNames[randomNameIndex()], randomCowWeight()));
72 break;
73 }
74 }
75
76 zoo.showAnimals(); // Display the animals
77
78 std::cout << "\nHerding and shearing all sheep..." << std::endl;

Callers

nothing calls this directly

Calls 5

addAnimalMethod · 0.45
showAnimalsMethod · 0.45
herdMethod · 0.45
shearMethod · 0.45

Tested by

no test coverage detected