Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created December 19, 2025 11:45
Show Gist options
  • Select an option

  • Save vlaleli/4c0497f9e7161406285103322f9a0818 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/4c0497f9e7161406285103322f9a0818 to your computer and use it in GitHub Desktop.
#include "Cat.h"
void Cat::run(double distance) const {
std::cout << "Кіт " << name << " біжить (" << distance << ")\n";
}
void Cat::jump(double height) const {
std::cout << "Кіт " << name << " стрибає (" << height << ")\n";
}
#pragma once
#include "Participant.h"
class Cat : public Participant {
public:
Cat(const std::string& name, double maxRun, double maxJump)
: Participant(name, maxRun, maxJump) {}
void run(double distance) const override;
void jump(double height) const override;
};
#include "Human.h"
void Human::run(double distance) const {
std::cout << "Людина " << name << " біжить (" << distance << ")\n";
}
void Human::jump(double height) const {
std::cout << "Людина " << name << " стрибає (" << height << ")\n";
}
#pragma once
#include "Participant.h"
class Human : public Participant {
public:
Human(const std::string& name, double maxRun, double maxJump)
: Participant(name, maxRun, maxJump) {}
void run(double distance) const override;
void jump(double height) const override;
};
#include <iostream>
#include <vector>
#include <memory>
#include "Human.h"
#include "Cat.h"
#include "Robot.h"
#include "Treadmill.h"
#include "Wall.h"
int main() {
std::vector<std::shared_ptr<Participant>> participants;
participants.push_back(std::make_shared<Human>("Олег", 600.0, 1.2));
participants.push_back(std::make_shared<Cat>("Барсик", 300.0, 2.3));
participants.push_back(std::make_shared<Robot>("RX-7", 1200.0, 0.7));
std::vector<std::shared_ptr<Obstacle>> obstacles;
obstacles.push_back(std::make_shared<Treadmill>(250.0));
obstacles.push_back(std::make_shared<Wall>(1.0));
obstacles.push_back(std::make_shared<Treadmill>(700.0));
obstacles.push_back(std::make_shared<Wall>(2.0));
for (const auto& p : participants) {
bool active = true;
for (const auto& o : obstacles) {
if (!active) break;
bool passed = o->tryPass(*p);
if (!passed) active = false;
}
std::cout << "------------------------------\n";
}
return 0;
}
#pragma once
#include <string>
#include "Participant.h"
class Obstacle {
protected:
std::string title;
public:
Obstacle(const std::string& title) : title(title) {}
virtual ~Obstacle() = default;
const std::string& getTitle() const { return title; }
virtual bool tryPass(const Participant& p) const = 0;
};
#pragma once
#include <string>
#include <iostream>
class Participant {
protected:
std::string name;
double maxRun;
double maxJump;
public:
Participant(const std::string& name, double maxRun, double maxJump)
: name(name), maxRun(maxRun), maxJump(maxJump) {}
virtual ~Participant() = default;
const std::string& getName() const { return name; }
double getMaxRun() const { return maxRun; }
double getMaxJump() const { return maxJump; }
virtual void run(double distance) const = 0;
virtual void jump(double height) const = 0;
};
#include "Robot.h"
void Robot::run(double distance) const {
std::cout << "Робот " << name << " біжить (" << distance << ")\n";
}
void Robot::jump(double height) const {
std::cout << "Робот " << name << " стрибає (" << height << ")\n";
}
#pragma once
#include "Participant.h"
class Robot : public Participant {
public:
Robot(const std::string& name, double maxRun, double maxJump)
: Participant(name, maxRun, maxJump) {}
void run(double distance) const override;
void jump(double height) const override;
};
#include "Treadmill.h"
#include <iostream>
bool Treadmill::tryPass(const Participant& p) const {
p.run(length);
if (p.getMaxRun() >= length) {
std::cout << "Учасник " << p.getName()
<< " пройшов перешкоду " << title
<< " на дистанції " << length << "\n";
return true;
}
std::cout << "Учасник " << p.getName()
<< " не пройшов перешкоду " << title
<< " на дистанції " << length
<< ". Пройдено " << p.getMaxRun() << "\n";
return false;
}
#pragma once
#include "Obstacle.h"
class Treadmill : public Obstacle {
double length;
public:
Treadmill(double length)
: Obstacle("Бігова доріжка"), length(length) {}
double getLength() const { return length; }
bool tryPass(const Participant& p) const override;
};
#include "Wall.h"
#include <iostream>
bool Wall::tryPass(const Participant& p) const {
p.jump(height);
if (p.getMaxJump() >= height) {
std::cout << "Учасник " << p.getName()
<< " пройшов перешкоду " << title
<< " на висоті " << height << "\n";
return true;
}
std::cout << "Учасник " << p.getName()
<< " не пройшов перешкоду " << title
<< " на висоті " << height
<< ". Пройдено " << p.getMaxJump() << "\n";
return false;
}
#pragma once
#include "Obstacle.h"
class Wall : public Obstacle {
double height;
public:
Wall(double height)
: Obstacle("Стіна"), height(height) {}
double getHeight() const { return height; }
bool tryPass(const Participant& p) const override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment