#include #include using namespace std; class Behaviour { public: Behaviour() = default; virtual ~Behaviour() = default; virtual int apply(int target) = 0; }; class Add : public Behaviour { public: Add(int value) : value{value} {} virtual int apply(int target) override { return target += value; } private: int value; }; class Square : public Behaviour { public: Square() {} virtual int apply(int target) override { return target * target; } }; int main() { int value{0}; Add a1{3}; Square s1{}; Add a2{1}; Square s2{}; std::vector behaviours { &a1, &s1, &a2, &s2 }; for ( Behaviour* behaviour_pointer : behaviours ) { value = (*behaviour_pointer).apply(value); } std::cout << value << std::endl; }