1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <random>
#include "animal_constants.h"
class Animal
{
public:
static int COUNTER;
protected:
int serial_no{-9};
int species{-9};
int age{0};
int gender{-9};
int reproduction_cycle{-9};
public:
Animal();
void setSerial();
void ageByMonth();
int getSpecies() { return species; }
bool getBreed();
friend std::ostream& operator<<(std::ostream&, Animal&);
};
int Animal::COUNTER = 0;
Animal::Animal(){setSerial();}
void Animal::setSerial(){serial_no = COUNTER++;}
void Animal::ageByMonth()
{
int temp = age;
age = temp + 1;
}
bool Animal::getBreed()
{
if(
(age > 0) and
(gender == FEMALE) and
(age % Cycle_time[reproduction_cycle] == 0)
)
{
return true;
}
else
{
return false;
}
}
std::ostream& operator<<(std::ostream& out, Animal& a)
{
return out
<< '#' << std::setw(3) << std::right << a.serial_no << ' '
<< std::setw(10) << std::left << Species_name[a.species] << ' '
<< std::setw(6) << std::left << Gender_name[a.gender] << ' '
<< std::setw(3) << std::right << Cycle_time[ a.reproduction_cycle ]
<< std::setw(3) << std::right << a.age << ' '
<< a.getBreed();
}
class Mammal: public Animal
{
public:
Mammal(){ reproduction_cycle = MAMMAL;};
};
class Cat: public Mammal
{
public:
Cat(Gender g, int aAge){gender = g; age = aAge; species = CAT;}
};
class Dog: public Mammal
{
public:
Dog(Gender g, int aAge){gender = g; age = aAge; species = DOG;}
};
class Fish: public Animal
{
public:
Fish(){species = FISH; reproduction_cycle = FISH;}
};
class Goldfish: public Fish
{
public:
Goldfish(Gender g, int aAge){gender = g; age = aAge; species = GOLDFISH;}
};
class Shark: public Fish
{
public:
Shark(Gender g, int aAge){gender = g; age = aAge; species = SHARK;}
};
class Bird: public Animal
{
public:
Bird(){species = BIRD; reproduction_cycle = BIRD;}
};
class Eagle: public Bird
{
public:
Eagle(Gender g, int aAge){gender = g; age = aAge; species = EAGLE;}
};
class Parakeet: public Bird
{
public:
Parakeet(Gender g, int aAge){gender = g; age = aAge; species = PARAKEET;}
};
#include <ctime>
class Ark:public Animal
{
private:
std::vector<Animal*> boat;
public:
Ark();
void display();
void add(Animal&);
void ageArkByMonth();
};
Ark::Ark(){ srand (time(nullptr)); }
void Ark::display()
{
std::cout << "*** ZOO CONTENTS ***\n";
for(auto i: boat)
std::cout << *i << '\n';
std::cout << '\n';
}
void Ark::add(Animal& a){boat.push_back(&a);}
void Ark::ageArkByMonth()
{
for(auto i: boat)
{
i->ageByMonth();
int gndr = rand()%2;
if( i->getBreed() )
{
switch(i -> getSpecies())
{
case CAT:
{
Cat* pCat = new Cat(static_cast<Gender>(gndr), 0);
add(*pCat);
break;
}
case DOG:
{
Dog* pDog = new Dog(static_cast<Gender>(gndr), 0);
add(*pDog);
break;
}
case GOLDFISH:
{
Goldfish* pGoldfish = new Goldfish(static_cast<Gender>(gndr), 0);
add(*pGoldfish);
break;
}
case SHARK:
{
Shark* pShark = new Shark(static_cast<Gender>(gndr), 0);
add(*pShark);
break;
}
case EAGLE:
{
Eagle* pEagle = new Eagle(static_cast<Gender>(gndr), 0);
add(*pEagle);
break;
}
case PARAKEET:
{
Parakeet* pParakeet = new Parakeet(static_cast<Gender>(gndr), 0);
add(*pParakeet);
break;
}
default:
std::cout << "No such beast\n";
}
}
}
}
int main()
{
Cat cat0(MALE, 10), cat1(FEMALE,11);
Dog dog0(FEMALE,10), dog1(MALE,10);
Goldfish goldfish0(FEMALE, 5), goldfish1(MALE,4);
Shark shark0(MALE,6), shark1(FEMALE,6);
Eagle eagle0(FEMALE,7), eagle1(MALE,10);
Parakeet parakeet0(FEMALE,8),parakeet1(FEMALE,6);
Ark noah;
noah.add(cat0);
noah.add(cat1);
noah.add(dog0);
noah.add(dog1);
noah.add(goldfish0);
noah.add(goldfish1);
noah.add(shark0);
noah.add(shark1);
noah.add(eagle0);
noah.add(eagle1);
noah.add(parakeet0);
noah.add(parakeet1);
noah.display();
for(int i = 0; i < 6; i++)
{
std::cout << "MONTH: " << i << '\n';
noah.ageArkByMonth();
noah.display();
}
return 0;
}
| |