官术网_书友最值得收藏!

Composition

When using composition, this is a strong HAS-A relationship, and this is when a class contains one or more instances of another class. Unlike aggregation, these instances are not created on their own but, instead, are created in the constructor of the class and then destroyed by its destructor. Put into layman's terms, they can't exist separately from the whole.

In this case, we have created some new properties for the Enemy class, adding in combat skills that it can use, as in the Pokémon series. In this case, for every one enemy, there are four skills that the enemy will be able to have:

class AttackSkill 
{
public:
void UseAttack(void);
private:
int damage;
float cooldown;
};

class Enemy
{
public:
void GetHealth(void) const;
void SetHealth(int);
private:
int currentHealth;
int maxHealth;
AttackSkill skill1;
AttackSkill skill2;
AttackSkill skill3;
AttackSkill skill4;
};

The line in the diagram looks similar to aggregation, aside from the fact that the diamond is filled in:

主站蜘蛛池模板: 常德市| 龙口市| 汽车| 惠州市| 林周县| 太仓市| 徐水县| 岳普湖县| 肇东市| 安义县| 五河县| 乐安县| 方正县| 琼结县| 锡林郭勒盟| 阿合奇县| 蕉岭县| 改则县| 淮阳县| 稷山县| 铜川市| 余姚市| 乐昌市| 潞城市| 定边县| 北流市| 顺平县| 沈阳市| 潍坊市| 龙南县| 嘉祥县| 宁夏| 潞城市| 治多县| 吴江市| 安阳市| 康马县| 临安市| 德州市| 缙云县| 咸宁市|