Please write code for this Aggregation assignment (C++): Step 1 - Define 2 class
ID: 3678701 • Letter: P
Question
Please write code for this Aggregation assignment (C++):
Step 1 - Define 2 classes. Above int main()
Create a class called PLAYER
string name
getName()
setName( string )
default constructor - set name to 'Unknown'
parm constructor - set name to one name on list of names below
Create a class Called TEAM
player * ptrPlayer1.... * ptrPlayer7
( Aggregation - use one pointer for each player )
printAllNames()
default constructor - assign each pointer to a player to null
Step 2 - Declare instances of classes. In int main()
Instantiate 7 players
Use parm constructor on the first 4 player names
Use the default constructor then use setNames on the last 3 player names
Use the following names: George, Ivan, Hang, Tuyet, Sue, Victoria and Tumbo
Instantiate two teams: basket ball Team , Soccer Team
Step 3 - Use classes. In int main()
Set the last 3 player names using setNames
Add any 5 of the player to the basket ball team, one at a time (5 statements - pass player pointer )
Add all 7 of the players to the soccer team ( 7 statements - pass player pointer )
Print out all the members’ names in the basket ball team, use printAllNames()
Print out all the members’ names in the soccer team, use printAllNames()
Delete the Basket ball team
Print out all names in the Basket Ball team
Delete the Soccer Team.
Print out all player names.
Delete each player
Explanation / Answer
I have used a array of player pointers in team class.If you wan't some other approach there comment on the answer
game.cpp
#include <iostream>
#include <string>
using namespace std;
class player
{
private:
string name;
public:
player(){name="Unknown";}
player(string pName){name=pName;}
string getName(){return name;}
void setName(string pName){name=pName;}
};
class team
{
public:
player* players[7];
int count=0;
team()
{
for(int i=0;i<7;++i)
{
players[i]=nullptr;
}
}
void addPlayer(player* p)
{
if(count>=7)
{
cout<<"Maximum of seven players can be added to a team"<<endl;
return;
}
players[count++]=p;
}
void printAllNames()
{
for(int i=0;i<7;++i)
{
if(players[i]!=nullptr)
{
cout<<players[i]->getName()<<endl;
}
}
}
};
int main()
{
player* player1=new player("George");
player* player2=new player("Ivan");
player* player3=new player("Hang");
player* player4=new player("Tuyet");
player* player5=new player();
player* player6=new player();
player* player7=new player();
player5->setName("Sue");
player6->setName("Victoria");
player7->setName("Tumbo");
team* basketBallTeam=new team();
team* soccerTeam=new team();
basketBallTeam->addPlayer(player1);
basketBallTeam->addPlayer(player2);
basketBallTeam->addPlayer(player3);
basketBallTeam->addPlayer(player4);
basketBallTeam->addPlayer(player5);
soccerTeam->addPlayer(player1);
soccerTeam->addPlayer(player2);
soccerTeam->addPlayer(player3);
soccerTeam->addPlayer(player4);
soccerTeam->addPlayer(player5);
soccerTeam->addPlayer(player6);
soccerTeam->addPlayer(player7);
basketBallTeam->printAllNames();
cout<<endl;
soccerTeam->printAllNames();
delete basketBallTeam;
basketBallTeam->printAllNames();
delete soccerTeam;
soccerTeam->printAllNames();
delete player1;
delete player2;
delete player3;
delete player4;
delete player5;
delete player6;
delete player7;
}