#include #include using namespace std; class Behaviour; class Object { public: Object(); void add_behaviour(Behaviour* behaviour); void simulate(); void set_x(int i); void set_y(int i); int get_x() const; int get_y() const; private: int x; int y; vector behaviours; }; class Behaviour { public: Behaviour() = default; virtual ~Behaviour() = default; virtual void execute(Object & object) = 0; }; class Move : public Behaviour { public: Move() = default; virtual void execute(Object & object) override { object.set_x(object.get_x() + 1); object.set_y(object.get_y() + 1); } }; class Display : public Behaviour { public: Display() = default; virtual void execute(Object & object) override { cout << object.get_x() << ", " << object.get_y() << endl; } }; Object::Object() : x{0}, y{0}, behaviours{} {} void Object::add_behaviour(Behaviour* behaviour) { behaviours.push_back(behaviour); } void Object::simulate() { for (Behaviour* behaviour: behaviours) { behaviour -> execute(*this); } } void Object::set_x(int i) { x = i; } void Object::set_y(int i) { y = i; } int Object::get_x() const { return x; } int Object::get_y() const { return y; } int main() { Object o1{}; Move m1{}; Display d1{}; o1.add_behaviour(&m1); o1.add_behaviour(&d1); o1.simulate(); o1.simulate(); o1.simulate(); }