Created
January 13, 2026 03:44
-
-
Save ExtremeFine21/8287218f28e8540e50a58995fe221e41 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 Obstacle; | |
| class Participant { | |
| protected: | |
| string name; | |
| int maxRun; | |
| int maxJump; | |
| bool active = true; | |
| public: | |
| Participant(string n, int r, int j) : name(n), maxRun(r), maxJump(j) {} | |
| virtual void run(int dist) = 0; | |
| virtual void jump(int height) = 0; | |
| bool isActive() const { return active; } | |
| string getName() const { return name; } | |
| int getMaxRun() const { return maxRun; } | |
| int getMaxJump() const { return maxJump; } | |
| void fail() { active = false; } | |
| virtual ~Participant() {} | |
| }; | |
| class Human : public Participant { | |
| public: | |
| Human(string n, int r, int j) : Participant(n, r, j) {} | |
| void run(int dist) override { | |
| cout << "Людина " << name << " біжить\n"; | |
| } | |
| void jump(int height) override { | |
| cout << "Людина " << name << " стрибає\n"; | |
| } | |
| }; | |
| class Cat : public Participant { | |
| public: | |
| Cat(string n, int r, int j) : Participant(n, r, j) {} | |
| void run(int dist) override { | |
| cout << "Кіт " << name << " біжить\n"; | |
| } | |
| void jump(int height) override { | |
| cout << "Кіт " << name << " стрибає\n"; | |
| } | |
| }; | |
| class Robot : public Participant { | |
| public: | |
| Robot(string n, int r, int j) : Participant(n, r, j) {} | |
| void run(int dist) override { | |
| cout << "Робот " << name << " біжить\n"; | |
| } | |
| void jump(int height) override { | |
| cout << "Робот " << name << " стрибає\n"; | |
| } | |
| }; | |
| class Obstacle { | |
| protected: | |
| string title; | |
| int value; | |
| public: | |
| Obstacle(string t, int v) : title(t), value(v) {} | |
| virtual bool overcome(Participant* p) = 0; | |
| virtual ~Obstacle() {} | |
| }; | |
| class RunningTrack : public Obstacle { | |
| public: | |
| RunningTrack(int length) : Obstacle("Бігова доріжка", length) {} | |
| bool overcome(Participant* p) override { | |
| p->run(value); | |
| if (p->getMaxRun() >= value) { | |
| cout << "Учасник[" << p->getName() << "] пройшов перешкоду[" << title | |
| << "] на дистанції[" << value << "]\n"; | |
| return true; | |
| } else { | |
| cout << "Учасник[" << p->getName() << "] не пройшов перешкоду[" << title | |
| << "] на дистанції[" << value << "]. Пройдено[" << p->getMaxRun() << "]\n"; | |
| p->fail(); | |
| return false; | |
| } | |
| } | |
| }; | |
| class Wall : public Obstacle { | |
| public: | |
| Wall(int height) : Obstacle("Стіна", height) {} | |
| bool overcome(Participant* p) override { | |
| p->jump(value); | |
| if (p->getMaxJump() >= value) { | |
| cout << "Учасник[" << p->getName() << "] пройшов перешкоду[" << title | |
| << "] на висоті[" << value << "]\n"; | |
| return true; | |
| } else { | |
| cout << "Учасник[" << p->getName() << "] не пройшов перешкоду[" << title | |
| << "] на висоті[" << value << "]. Пройдено[" << p->getMaxJump() << "]\n"; | |
| p->fail(); | |
| return false; | |
| } | |
| } | |
| }; | |
| int main() { | |
| vector<Participant*> participants = { | |
| new Human("Олег", 500, 2), | |
| new Cat("Барсик", 300, 4), | |
| new Robot("R2D2", 1000, 1) | |
| }; | |
| vector<Obstacle*> obstacles = { | |
| new RunningTrack(200), | |
| new Wall(2), | |
| new RunningTrack(600), | |
| new Wall(3) | |
| }; | |
| for (auto p : participants) { | |
| for (auto o : obstacles) { | |
| if (!p->isActive()) break; | |
| o->overcome(p); | |
| } | |
| cout << "-----------------\n"; | |
| } | |
| for (auto p : participants) delete p; | |
| for (auto o : obstacles) delete o; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment