1

I've made a class called Player, whose goal is to create a new object player that has these attributes as below and vector of 5 strings (skills of player).

Player(string name, string surname, int height, vector<string> skills) {
    this->name = name;
    this->surname = surname;
    this->height = height;
    this->skills = skills;
}

I've also created a method that shows data of an object.

// Method showing data
string show_data() {
    stringstream ss;
    string s;

    ss <<
        this->name << " " <<
        this->surname << " " <<
        this->height << " ";
    for (int i = 0; i < 5; i++) {
        ss << this->skills[i] << " ";
    }
    ss << "\n";
    s = ss.str();
    return s;
}

My goal is to group 5 players into one team in another class. I've printed out the data of players

cout << "Liverpool\n" << p1.show_data() << p2.show_data() << p3.show_data() << p4.show_data() << p5.show_data() << endl

But I want to have something like cout << liverpool.show_data(); and it should print the data of all 5 players

cout << real_madrit.show_data(); etc..

The question arises, how can I pass these 5 objects to another class with their attributes?

4
  • 1
    Create a Team class that has a std::vector<Player>. Commented Mar 27, 2020 at 19:09
  • "how can I pass these 5 objects to another class with their attributes" - put the 5 objects in a container and pass the container. Commented Mar 27, 2020 at 19:10
  • 1
    Exactly the same way you do it with the player's skills? Commented Mar 27, 2020 at 19:11
  • It does seem strange to start with a class (Player) that groups 5 objects of a different class (std::string), then ask how to define a class that groups 5 objects of a different class. Where's the problem? Did you try it and run into a problem? Commented Mar 27, 2020 at 19:21

1 Answer 1

4

You can just create a new class that has Players.

class Team
{
private:

  std::vector<Player> players_;

public:

  Team(std::vector<Player> p) : players_(p) {}

  void show_data() const
  {
    for (auto const & player : players_)
      std::cout << player.show_data();
  } 
};

Now you can use this class like so.

std::vector<Player> p = /* create a bunch of players */
Team team(p); // create a Team
team.show_data(); // display the team

If you try to compile this example, you will get an error, because each player in the for loop in Team::show_data() is marked as a const-reference. You could remove the const here, but don't do that. (After all, showing the data of a Team/Player shouldn't modify the Team/Player). So instead, make your Player::show_data() a const method.

Note that this will allow teams of any number of players. If you definitely want just 5 per team, then your players_ member could be an array

std::array<Player, 5> players_;

If you see the Team class constructor, you might see the initializer list syntax. You could use that for your Player constructor as well, and it becomes

Player(string name, string surname, int height, vector<string> skills) 
 : name(name), surname(surname), height(height), skills(skills) {}
Sign up to request clarification or add additional context in comments.

8 Comments

And if it doesn't work its because Player::show_data needs to be const.
Ok it works :) Thank you very much! I have one more question, could you help me with creating copy constructor for class Team?
@Mefrex The Team class presented in this answer shouldn't need a user-defined copy constructor. The default one will do just fine.
Alright. In my homework I gotta change one attribute of any player. This task requires not having "const" in my methods. Could you help me rewrite show_data() method in a class Team?
You want to change a Player attribute inside show_data()? That's not good design, IMO
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.