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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include <iostream>
#include <memory>
class Animal
{
public:
enum class EyeColor { White, Gray, Yellow, Blue, Brown };
Animal
(
const std::string& name,
const std::string& gender,
const std::string type,
const std::string& pronoun,
double weight,
EyeColor eyeColor
)
:
mName(name),
mGender(gender),
mType(type),
mPronoun(pronoun),
mWeight(weight),
mEyeColor(eyeColor)
{}
virtual ~Animal() = default;
virtual void Speak() const
{
std::cout << "Default Speak\n";
}
virtual void AdditionalInfo() const
{
std::cout << "Default Info\n";
}
virtual void Move() const
{
std::cout << "Generic animal Movement\n";
}
virtual void Fall() const
{
std::cout << "Animal falls normally.\n";
}
std::string GetName() const
{
return mName;
}
double GetWeight() const
{
return mWeight;
}
std::string GetGender() const
{
return mGender;
}
std::string GetType() const
{
return mType;
}
std::string GetPronoun() const
{
return mPronoun;
}
EyeColor GetEyeColor() const
{
return mEyeColor;
}
private:
std::string mName{ "Animal Name" };
std::string mGender{ "None" };
std::string mType{ "Animal Type" };
std::string mPronoun{ "Animal Pronoun" };
double mWeight{ 0.0 };
EyeColor mEyeColor{ EyeColor::Gray };
};
class Cow : public Animal
{
public:
using Animal::Animal;
void Speak() const override
{
std::cout << "Moo.\n";
}
void Move() const override
{
std::cout << "Cows like " << GetName() << " walk slowly on 4 hooves.\n";
}
void AdditionalInfo() const override
{
std::cout << "Cows like " << GetName() << " like to graze for grass.\n";
}
};
class Dog : public Animal
{
public:
using Animal::Animal;
void Speak() const override
{
std::cout << "Bark.\n";
}
void Move() const override
{
std::cout << "Dogs like " << GetName() << " walk around on 4 paws.\n";
}
void AdditionalInfo() const override
{
std::cout << "Dogs like " << GetName() << " like to play fetch.\n";
}
};
class Cat : public Animal
{
public:
Cat
(
const std::string& name,
const std::string& gender,
const std::string type,
const std::string& pronoun,
double weight,
int lives,
EyeColor eyeColor
)
:
Animal{name, gender, type, pronoun, weight, eyeColor}, mLives(lives)
{}
void Speak() const override
{
std::cout << "Meow.\n";
}
void AdditionalInfo() const override
{
std::cout << "Cats like " << GetName() << " have " << mLives << " lives!" << "\n";
}
void Fall() const override
{
std::cout << "When cats like " << GetName() << " fall, they always land feet first!\n";
}
void Move() const override
{
std::cout << "Cats like " << GetName() << " walk around on 4 paws and can run fast.\n";
}
private:
int mLives{ 9 };
};
std::ostream& operator<<(std::ostream& os, Animal::EyeColor eyeColor)
{
switch (eyeColor)
{
case Animal::EyeColor::White:
{
os << "White";
break;
}
case Animal::EyeColor::Gray:
{
os << "Gray";
break;
}
case Animal::EyeColor::Yellow:
{
os << "Yellow";
break;
}
case Animal::EyeColor::Blue:
{
os << "Blue";
break;
}
case Animal::EyeColor::Brown:
{
os << "Brown";
break;
}
default:
std::cout << "Error\n";
}
return os;
}
void CheckForNull(const Animal* animal)
{
if (animal == nullptr)
{
std::cout << "Pointer has been reset to null!\n";
}
else
{
std::cout << "This animal is a " << animal->GetGender() << " " << animal->GetType() << " named " << animal->GetName();
std::cout << " and " << animal->GetPronoun() << " says ";
animal->Speak();
std::cout << animal->GetName() << " weighs " << animal->GetWeight() << " lbs and has " << animal->GetEyeColor() << " eyes.\n";
}
}
int main()
{
std::unique_ptr<Animal> wiskers{ std::make_unique<Cat>("Wiskers", "Male", "Cat", "He", 5, 9, Animal::EyeColor::Yellow ) };
std::unique_ptr<Animal> fido{ std::make_unique<Dog>("Fido", "Female", "Dog", "She", 20, Animal::EyeColor::Blue ) };
std::unique_ptr<Animal> bessie{ std::make_unique<Cow>("Bessie", "Female", "Cow", "She", 900, Animal::EyeColor::Brown ) };
//wiskers.reset();
CheckForNull(wiskers.get());
wiskers->AdditionalInfo();
wiskers->Fall();
wiskers->Move();
std::cout << '\n';
CheckForNull(fido.get());
fido->AdditionalInfo();
fido->Fall();
fido->Move();
std::cout << '\n';
CheckForNull(bessie.get());
bessie->AdditionalInfo();
bessie->Fall();
bessie->Move();
return 0;
}
|