1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <iostream>
using namespace std;
class BaseEnemy
{
protected:
int health;
int defensive;
std::string name;
BaseEnemy(int _h, int _d, std::string _n)
: health(_h), defensive(_d), name(_n) {};
public:
~BaseEnemy() {};
virtual int getHealth() const {return health;};
virtual int getDefensive() const {return defensive;};
virtual std::string getName() const {return name;};
virtual void getInformations() const
{
std::cout << "Vie : " << health << std::endl;
std::cout << "Defense : " << defensive << std::endl;
std::cout << "Nom : " << name << std::endl << std::endl;
};
};
class Enemy : public BaseEnemy
{
public:
Enemy(int _h, int _d, std::string _n)
: BaseEnemy(_h,_d, _n) {};
~Enemy(){};
};
class Decorator : public BaseEnemy
{
protected:
BaseEnemy& base;
Decorator(BaseEnemy& _base, int _h, int _d, std::string _n)
: BaseEnemy(_h, _d, _n), base(_base) {};
~Decorator(){ };
public:
BaseEnemy& getBase() const {return base;};
};
class PowerSkill : public Decorator
{
public:
int getHealth() const {return base.getHealth()+health;};
int getDefensive() const {return base.getDefensive()+defensive;};
std::string getName() const {return base.getName()+" "+name;};
void getInformations() const
{
std::cout << "Vie : " << getHealth() << std::endl;
std::cout << "Defense : " << getDefensive() << std::endl;
std::cout << "Nom : " << getName() << std::endl << std::endl;
};
PowerSkill(BaseEnemy& _base, int _h, int _d, std::string _n)
: Decorator(_base, _h, _d, _n) {};
~PowerSkill() {};
};
class FireSkill : public Decorator
{
private:
int powerFire;
public:
int getPowerFire() const {return powerFire; };
void getInformations() const
{
base.getInformations();
std::cout << "Puissance de feu : " << getPowerFire() << std::endl;
};
FireSkill(BaseEnemy& _base, int _powerFire)
: Decorator(_base, 0, 0, ""), powerFire(_powerFire) {};
~FireSkill() {};
};
int main()
{
BaseEnemy* b = new Enemy(5, 10, "Enemy");
b->getInformations();
b = new PowerSkill(*b, 2, 3, "Power Enemy");
b->getInformations();
b = new FireSkill(*b, 100);
b->getInformations();
delete b;
return 0;
} |
Partager