Created
March 11, 2026 12:31
-
-
Save vlaleli/61d6b8e39c188c3028636e61c71af114 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <string> | |
| using namespace std; | |
| class Herbivore { | |
| protected: | |
| int weight; | |
| bool life; | |
| public: | |
| Herbivore(int w) : weight(w), life(true) {} | |
| virtual ~Herbivore() {} | |
| virtual void EatGrass() = 0; | |
| virtual string GetName() const = 0; | |
| int GetWeight() const { | |
| return weight; | |
| } | |
| bool IsAlive() const { | |
| return life; | |
| } | |
| void SetLife(bool state) { | |
| life = state; | |
| } | |
| }; | |
| class Carnivore { | |
| protected: | |
| int power; | |
| public: | |
| Carnivore(int p) : power(p) {} | |
| virtual ~Carnivore() {} | |
| virtual void Eat(Herbivore* herbivore) = 0; | |
| virtual string GetName() const = 0; | |
| int GetPower() const { | |
| return power; | |
| } | |
| }; | |
| class Wildebeest : public Herbivore { | |
| public: | |
| Wildebeest() : Herbivore(50) {} | |
| void EatGrass() override { | |
| if (life) { | |
| weight += 10; | |
| cout << "Wildebeest eats grass. Weight = " << weight << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Wildebeest"; | |
| } | |
| }; | |
| class Bison : public Herbivore { | |
| public: | |
| Bison() : Herbivore(60) {} | |
| void EatGrass() override { | |
| if (life) { | |
| weight += 10; | |
| cout << "Bison eats grass. Weight = " << weight << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Bison"; | |
| } | |
| }; | |
| class Elk : public Herbivore { | |
| public: | |
| Elk() : Herbivore(70) {} | |
| void EatGrass() override { | |
| if (life) { | |
| weight += 10; | |
| cout << "Elk eats grass. Weight = " << weight << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Elk"; | |
| } | |
| }; | |
| class Lion : public Carnivore { | |
| public: | |
| Lion() : Carnivore(80) {} | |
| void Eat(Herbivore* herbivore) override { | |
| if (!herbivore->IsAlive()) { | |
| cout << "Lion cannot eat dead " << herbivore->GetName() << endl; | |
| return; | |
| } | |
| cout << "Lion attacks " << herbivore->GetName() << endl; | |
| if (power > herbivore->GetWeight()) { | |
| power += 10; | |
| herbivore->SetLife(false); | |
| cout << "Lion wins. Power = " << power << endl; | |
| } else { | |
| power -= 10; | |
| cout << "Lion loses. Power = " << power << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Lion"; | |
| } | |
| }; | |
| class Wolf : public Carnivore { | |
| public: | |
| Wolf() : Carnivore(65) {} | |
| void Eat(Herbivore* herbivore) override { | |
| if (!herbivore->IsAlive()) { | |
| cout << "Wolf cannot eat dead " << herbivore->GetName() << endl; | |
| return; | |
| } | |
| cout << "Wolf attacks " << herbivore->GetName() << endl; | |
| if (power > herbivore->GetWeight()) { | |
| power += 10; | |
| herbivore->SetLife(false); | |
| cout << "Wolf wins. Power = " << power << endl; | |
| } else { | |
| power -= 10; | |
| cout << "Wolf loses. Power = " << power << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Wolf"; | |
| } | |
| }; | |
| class Tiger : public Carnivore { | |
| public: | |
| Tiger() : Carnivore(90) {} | |
| void Eat(Herbivore* herbivore) override { | |
| if (!herbivore->IsAlive()) { | |
| cout << "Tiger cannot eat dead " << herbivore->GetName() << endl; | |
| return; | |
| } | |
| cout << "Tiger attacks " << herbivore->GetName() << endl; | |
| if (power > herbivore->GetWeight()) { | |
| power += 10; | |
| herbivore->SetLife(false); | |
| cout << "Tiger wins. Power = " << power << endl; | |
| } else { | |
| power -= 10; | |
| cout << "Tiger loses. Power = " << power << endl; | |
| } | |
| } | |
| string GetName() const override { | |
| return "Tiger"; | |
| } | |
| }; | |
| class Continent { | |
| public: | |
| virtual ~Continent() {} | |
| virtual Herbivore* CreateHerbivore() = 0; | |
| virtual Carnivore* CreateCarnivore() = 0; | |
| }; | |
| class Africa : public Continent { | |
| public: | |
| Herbivore* CreateHerbivore() override { | |
| return new Wildebeest(); | |
| } | |
| Carnivore* CreateCarnivore() override { | |
| return new Lion(); | |
| } | |
| }; | |
| class NorthAmerica : public Continent { | |
| public: | |
| Herbivore* CreateHerbivore() override { | |
| return new Bison(); | |
| } | |
| Carnivore* CreateCarnivore() override { | |
| return new Wolf(); | |
| } | |
| }; | |
| class Eurasia : public Continent { | |
| public: | |
| Herbivore* CreateHerbivore() override { | |
| return new Elk(); | |
| } | |
| Carnivore* CreateCarnivore() override { | |
| return new Tiger(); | |
| } | |
| }; | |
| class AnimalWorld { | |
| private: | |
| vector<Herbivore*> herbivores; | |
| vector<Carnivore*> carnivores; | |
| public: | |
| AnimalWorld(Continent* continent, int count = 3) { | |
| for (int i = 0; i < count; i++) { | |
| herbivores.push_back(continent->CreateHerbivore()); | |
| carnivores.push_back(continent->CreateCarnivore()); | |
| } | |
| } | |
| ~AnimalWorld() { | |
| for (size_t i = 0; i < herbivores.size(); i++) { | |
| delete herbivores[i]; | |
| } | |
| for (size_t i = 0; i < carnivores.size(); i++) { | |
| delete carnivores[i]; | |
| } | |
| } | |
| void MealsHerbivores() { | |
| cout << "Herbivores are eating:" << endl; | |
| for (size_t i = 0; i < herbivores.size(); i++) { | |
| herbivores[i]->EatGrass(); | |
| } | |
| } | |
| void NutritionCarnivores() { | |
| cout << "Carnivores are hunting:" << endl; | |
| for (size_t i = 0; i < carnivores.size(); i++) { | |
| carnivores[i]->Eat(herbivores[i]); | |
| } | |
| } | |
| void ShowState() { | |
| cout << "Herbivores:" << endl; | |
| for (size_t i = 0; i < herbivores.size(); i++) { | |
| cout << herbivores[i]->GetName() | |
| << " Weight = " << herbivores[i]->GetWeight() | |
| << " Life = " << (herbivores[i]->IsAlive() ? "Alive" : "Dead") << endl; | |
| } | |
| cout << "Carnivores:" << endl; | |
| for (size_t i = 0; i < carnivores.size(); i++) { | |
| cout << carnivores[i]->GetName() | |
| << " Power = " << carnivores[i]->GetPower() << endl; | |
| } | |
| } | |
| }; | |
| int main() { | |
| Africa africa; | |
| NorthAmerica northAmerica; | |
| Eurasia eurasia; | |
| cout << "AFRICA" << endl; | |
| AnimalWorld world1(&africa); | |
| world1.ShowState(); | |
| world1.MealsHerbivores(); | |
| world1.NutritionCarnivores(); | |
| world1.ShowState(); | |
| cout << endl; | |
| cout << "NORTH AMERICA" << endl; | |
| AnimalWorld world2(&northAmerica); | |
| world2.ShowState(); | |
| world2.MealsHerbivores(); | |
| world2.NutritionCarnivores(); | |
| world2.ShowState(); | |
| cout << endl; | |
| cout << "EURASIA" << endl; | |
| AnimalWorld world3(&eurasia); | |
| world3.ShowState(); | |
| world3.MealsHerbivores(); | |
| world3.NutritionCarnivores(); | |
| world3.ShowState(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment