I am coding a class project where I will have a basketball league. The league will include 3 divisions and each division will have 5 teams. Each team will have a number of players and one coach. I must have 5 seperate classes. I already have two classes called person and player and will also add coach. I think my next two classes will be division and team. My question is how should I implement these two classes? I must be able to add players and a coach to a team and add teams to a division. Should I use arrays for only 3 divisions and for the teams? I'm just a little confused on how to set everything up and how everything will interact.

Dani AI

Generated

For 's assignment (Person, Player, Coach, Team, Division) the simplest, most maintainable approach is composition: one League contains Division instances, each Division contains Team instances, and each Team holds its Coach and a collection of Players. That avoids creating a separate subclass for Division1, Division2, etc., which is what suggested — inheritance is for different behavior, not for creating numbered types.

A compact C++-style skeleton showing the idea:

struct Person { std::string name; int id; /* common fields */ };
struct Player : Person { int jersey; /* stats */ };
struct Coach  : Person { int years; /* coach data */ };

class Team {
  std::vector<Player> players;
  std::unique_ptr<Coach> coach; // nullptr until set
public:
  void addPlayer(const Player& p) { players.push_back(p); }
  bool removePlayerById(int id);
  void setCoach(std::unique_ptr<Coach> c) { coach = std::move(c); }
  const std::vector<Player>& getPlayers() const { return players; }
};

class Division { std::vector<Team> teams; void addTeam(Team t) { teams.push_back(std::move(t)); } };
class League   { std::vector<Division> divisions; };

Practical notes and gaps to fill from earlier replies:

  • Use std::vector for flexible sizes; switch to std::array<T,5> or raw arrays only if the spec forces fixed sizes.
  • Decide ownership clearly: players usually belong to one Team (store by value or unique_ptr). Coach is typically unique to a Team (unique_ptr or value).
  • Provide small APIs: add/remove by id, findPlayerById, getTeams(), and const accessors so callers don't mutate internals.
  • Test copy/move behavior (vectors move; unique_ptr does not copy). Avoid exposing internal containers for modification.
  • Keep business rules (max roster size, valid jersey numbers) inside Team, not sprinkled across code.

This design keeps the object graph simple, meets the five-class requirement, and is easy to extend (standings, schedules, stats) without exploding the class hierarchy.

Recommended Answers

All 2 Replies

I am coding a class project where I will have a basketball league. The league will include 3 divisions and each division will have 5 teams. Each team will have a number of players and one coach. I must have 5 seperate classes. I already have two classes called person and player and will also add coach. I think my next two classes will be division and team. My question is how should I implement these two classes? I must be able to add players and a coach to a team and add teams to a division. Should I use arrays for only 3 divisions and for the teams? I'm just a little confused on how to set everything up and how everything will interact.

I'll provide a ascii diagram of how you might implement this and a following explaination:

[  class_league ]
                        [  class_division ]  [  class_team  ]  [  class_player  ]
                                     |       |       |               |___________      |
                                     |       |       |_[  class_division1  ] |   |     |___[  class_player1  
                                     |       |_____[  class_division2  ] |   |     |___[  class_player2
                                     |_________[  class_division3  ] |   |     |___[  class_player3  
                                     [  class_team1  ]______________|     .... player up to 5
                                     [  class_team2  ]______________|
                                     [  class_team3  ]______________|
                                     .... team division up to 5

There will be one class league.
There will be a base class, perhaps an ADT (i.e. pure virtual) class division and inhereted class divisionN where N is the sucessive number up to the number of divisions you desire.
There will be a base class team, also maybe ADT, team and inherited teams.
There will be a base class player and follows the same as the above description of base classes.

The point is, is classes have commonalities, these commonalities (i.e. functions and/or rules) will be implemented or specified for the base classes of the inherited classes. You'll have to figure the distinctions for each inherited class and their commonalities. Just some ideas.

Good luck, LamaBot

Referring to my previous post reply, I'll give an example of the distinctions and commonalites. Each team could have uniforms. The uniforms for each team have a basic structure yet have a different logo design. The basic structure might be specified in the base class of the team or just have a base class for the uniform. Each teams logo would be used in the inherited class which inherit the basic structure of the uniform but add some unique logo specific for that team.

Good luck, LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.