Answer to Question #333537 in C++ for Harsh

Question #333537

There is a class Team, that holds information about the team name, number of players in the team, and player’s code. Design the constructor function that decides the number of player in the team and initializes code for each player. Create a team and display its all data. (Implement dynamic memory allocation)

1
Expert's answer
2022-04-25T13:36:56-0400
#include <iostream>
#include <string>

using namespace std;

class Team
{
	string name;
	int numPlys;
	int* PlysCodes;
public:
	Team(string _name, int _numPlys):name(_name),numPlys(_numPlys)
	{
		PlysCodes = new int[numPlys];
		cout << "Please, enter players codes:\n";
		for (int i = 0; i < numPlys; i++)
		{
			cout << "Player`s " << i + 1 << " code: ";
			cin >> PlysCodes[i];
		}
	}
	void Display()
	{
		cout << "\nInfo about team: "
			<< "\nName:" << name << endl;
		for (int i = 0; i < numPlys; i++)
		{
			cout << "Code of player " << i + 1 << ": ";
			cout << PlysCodes[i] << endl;
		}
	}
};


int main()
{
	Team a("Liverpool", 11);
	a.Display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS