Comprehending composition failure!

I'm trying to help a friend with a c++ class assessment, and am stuck at the last part where I need to create a Team class using composition. I've done some basic C++ coding before but haven't used composition before (just some inheritance and friend classes)

The Team class and needs to have the following.
- string teamName; // The name of the team
- Player player[2]; // An array of two player objects

A Team also has the following public methods:
- Setters and getters for the teamName property:
o void setInjured(bool injuredValue), and
o bool getInjured()
- A function called displayTeam() which prints out to the console:
o The name of the team,
o All properties (name, age, number, injured) of each player in the team.

I'm also having issues working with the player object array in the Team class, any and all help would be greatly appreciated. I've been researching online for a while now but keep going in circles :( I've placed my current code below.

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
#ifndef PERSON_CLASS
#define PERSON_CLASS

#include <string>
using namespace std;

class Person
{
private:

	string name;
	int age;

public:

	Person();

	void setName(string theName);
	string getName();

	void setAge(int theirAge);
	int getAge();
};

Person::Person()
{
	name = "NAME NOT SET";
	age = -1;
}

void Person::setName(string theName)
{
	name = theName;
}

string Person::getName()
{
	return name;
}

void Person::setAge(int theirAge)
{
	age = theirAge;
}

int Person::getAge()
{
	return age;
}
#endif 


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
#ifndef PLAYER_CLASS
#define PLAYER_CLASS

#include <iostream>
#include "Person.h"

class Player : public Person
{
private:

	int number;
	bool injured;

public:

	Player();

	void setNumber(int theNumber);
	int getNumber();

	void setInjured(bool injuredValue);
	bool getInjured();

	void displayPlayer();
};

Player::Player()
{
	number = -1;
	injured = false;
}

void Player::displayPlayer()
{
	cout<<"Player name: "<<getName()<<endl;
	cout<<"Player age: "<<getAge()<<endl;
	cout<<"Injury status: "<<injured<<endl;
	cout<<"Number: "<<number<<endl;
}

void Player::setNumber(int theNumber)
{
	number = theNumber;
}

int Player::getNumber()
{
	return number;
}

void Player::setInjured(bool injuredValue)
{
	injured = injuredValue;
}

bool Player::getInjured()
{
	return injured;
}
#endif 


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
#ifndef TEAM_CLASS
#define TEAM_CLASS

#include <string>
#include "Player.h"

class Team
{
private:
	string teamName;
	Player player[2];

public:	
	void setInjured(bool injuredValue)
	{
		player[0].setInjured(injuredValue);
	}
	bool getInjured()
	{
		player[0].getInjured();
	}
	//void setInjured(bool injuredValue);
	//bool getInjured();
	Team();

	void setTeamName(string theTeamName);
	string getTeamName();

	void displayTeam();
};

Team::Team()
{

}

void Team::displayTeam()
{
	cout<<"Team name: "<<teamName<<endl;
	//cout<<"Player name: "<<getName()<<endl;
	//cout<<"Player age: "<<getAge()<<endl;
	//cout<<"Injury status: "<<injured<<endl;
	//cout<<"Number: "<<number<<endl;
}

void Team::setTeamName(string theTeamName)
{
	teamName = theTeamName;
}

string Team::getTeamName()
{
	return teamName;
}
#endif 


Thanks for looking this far! :)
Your Person and Player classes look fine.

Why does Team have setInjured and getInjured? These are attributes of Player.

In displayTeam, you need to display each Player instance.
1
2
3
4
5
void Team::displayTeam()
{  cout<<"Team name: "<<teamName<<endl;
    player[0].DisplayPlayer();
    player[1].DisplayPlayer();
}



Thanks very much for the reply :) I wondered that myself, it's what the criteria for the assessment say and it made me think it was something to do with the 'composition' part.

I've changed the Team class so that it's functional using inheritance, but still not sure how to do this using composition.

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
#ifndef TEAM_CLASS
#define TEAM_CLASS

#include <string>
#include "Player.h"

class Team : public Player
{
private:
	string teamName;
	Player player[2];

public:	
	/*void setInjured(bool injuredValue)
	{
		player[0].setInjured(injuredValue);
	}
	bool getInjured()
	{
		player[0].getInjured();
	}*/
	//void setInjured(bool injuredValue);
	//bool getInjured();
	Team();

	void setTeamName(string theTeamName);
	string getTeamName();

	void setPlayerData(bool playerCode, string name, int age, bool injured, int number);

	void displayTeam();
};

Team::Team()
{
}

void Team::setPlayerData(bool playerCode, string name, int age, bool injured, int number)
{
	player[playerCode].setName(name);
	player[playerCode].setAge(age);
	player[playerCode].setInjured(injured);
	player[playerCode].setNumber(number);
}

void Team::displayTeam()
{
	cout<<"Team name: "<<teamName<<endl;
	player[0].displayPlayer();
	player[1].displayPlayer();
}

void Team::setTeamName(string theTeamName)
{
	teamName = theTeamName;
}

string Team::getTeamName()
{
	return teamName;
}
#endif 
Topic archived. No new replies allowed.