Object relationship

18.4 - Aggregation


Aggregation is another type of object relationship. In aggregation, the object must have the members to exist, but the members do not need to have the object, or even know the object.

For example, to be a Pokemon trainer you must have a Pokemon (or a lot of them). But a Pokemon does not need to have a trainer, it may as well be a wild Pokemon.

Your task is to complete the Pokemon and Trainer class.

The Pokemon class has the following members:

  • name (string)
  • type (string)
  • ability (string)
  • info (void function, which prints out <name> is a <type> type Pokemon with the ability <ability>.)

The Trainer class has the following members:

  • name (string)
  • party (vector of Pokemon*)
  • info (void function, which prints out <name> has the following Pokemon in his/her party: and then the Pokemon info each line after that.)

Sample code input

int main()
{
    Pokemon *pikachu = new Pokemon{"Pikachu", "Electric", "Static"};
    Trainer *ash = new Trainer{"Ash", {pikachu}};
    ash->info();
}

Sample output

Ash has the following Pokemon in his/her party:
Pikachu is a Electric type Pokemon with the ability Static.
#include <iostream> #include <string> #include <vector> class Pokemon { public: }; class Trainer { public: };