For this assignment, implement and use the methods for a class called Player. Th
ID: 3547256 • Letter: F
Question
For this assignment, implement and use the methods for a class called Player. This must run using Dev C++.
no microsoft visual studio or anything like that.
Use the following class definition:
The data members for the class are:
name this holds the player's name
goals this holds the number of goals that the player has scored
assists this holds the number of assists that the player has earned
rating this holds the player's plus-minus rating
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the name to the null string (this can be done by assigning '' to the 0th spot of the character array or by using strcpy to copy an empty string ("") into the character array) and the integer data members to 0.
The other constructor for the class should initialize the data members using the passed in arguments. It takes 4 arguments: a character array with a player name, an integer that holds the number of goals that the player has scored, an integer that holds the number of assists the player has earned, and an integer that holds the player's plus-minus rating. The numeric values can be initialized with simple assignment statements or by calling the set methods for the numeric data members. If the set methods are going to be used, don't forget to set the individual data members to 0 before calling the appropriate set method. Use thestrcpy functio to initialize the name or call the setName method.
This method displays the player information and the number of points. It takes no arguments and returns nothing. The number of points is calculated as follows:
The information should be displayed as follows:
Notice that the + sign is printing in front of the positive plus/minus rating. - signs print by default. To get + sign to display, set the showpos flag before displaying the plus-minus rating. To turn off the display of + signs, set the noshowpos flag after displaying the plus-minus rating.
This method changes a player's name. It takes one argument: an array of characters that represents the player's name. It returns nothing.
This method changes a player's number of goals. It takes one argument: an integer that represents the number of goals that have been scored by the player. It returns nothing.
If the passed in number of goals that have been scored is less than 0, display an error message and do not make any changes to the goals data member. If the passed in number of goals that have been scored is greater than or equal to 0, increment the goals data member by the passed in number of goals that have been scored.
This method changes a player's number of assists. It takes one argument: an integer that represents the number of assists that have been earned by the player. It returns nothing.
If the passed in number of assists that have been earned is less than 0, display an error message and do not make any changes to the assists data member. If the passed in number of assists that have been earned is greater than or equal to 0, increment the assists data member by the passed in number of assists that have been earned.
This method changes a player's plus-minus rating. It takes one argument: an integer that represents the change to the player's plus-minus rating. It returns nothing.
The passed in change to the player's plus-minus should be used to increment the rating data member.
Note: the plus-minus rating is a statistic that indicates if a player has been on the ice for more goals scored for their team (a positive rating) or more goals scored against their team (a minus rating).
This method returns a player's number of goals scored. It takes no arguments.
This method returns a player's number of assists earned. It takes no arguments.
This method returns a player's plus-minus rating. It takes no arguments.
In main(), create 6 Player objects. They should contain the values:
The first player should have your name, 1 goal scored, 1 assist, and a plus-minus rating of +1. Note: if you're pair programming, set the name to both you and your partner: "Jane Doe/John Doe".
The second player should be created using the default constructor (the one that doesn't take any arguments)
The third player should have the name "Jonathan Toews", 10 goals scored, 9 assists earned, and a plus-minus rating of +6
The fourth player should have the name "Patrick Kane", 11 goals scored, 10 assists earned, and a plus-minus rating of -3
The fifth player should have the name "Brandon Saad", 5 goals scored, 8 assists earned, and a plus-minus rating of +8
The sixth player should have the name "Brandon Pirri", 6 goals scored, 5 assists earned, and a plus-minus rating of +6
The rest of main() will include using the various methods on each of the 6 Player objects. Display a label similar to "The first Player object" before anything is outputted for each of the objects.
For the first player, display the player information, using the setGoals method, increase the player's number of goals by 2, and then display the player information once again.
For the second player, display the player information, set the player name to "Patrick Sharp", set the number of goals scored to 7, set the number of assists to 13, set the plus-minus rating to +10, and then display the player information once again.
For the third player, display the player's information, using the setGoals method, try to increase the player's number of goals by -8, using the setAssists method, try to increase the player's number of assists by -2, and then display the player information once again.
For the fourth player, display the player's information, using setAssists, increase the number of assists by 3, using setRating, change the plus-minus rating by +3, and then display the player information once again.
For the fifth player, display only the player's number of goals and plus-minus rating, and then display all of the player information once again.
For the sixth player, display the player's information, using setAssists, try to increase the number of assists by -2, using setAssists, increase the number of assists by 4, display only the number of assists that the sixth player has earned, using setRating, change the plus-minus rating by -4, and then display the player information once again.
Some compilers may issue a warning on the constructor that takes 4 arguments and the setName method because of the char [] argument and the fact that string literals are being passed in. The warning usually includes something about "deprecated conversion from string constant to char *". If this happens, change the "char []" to "const char []" and make sure that the const is also on the header.
Each method must have a documentation box like a function.
Hand in a copy of your source code using Blackboard. As noted above, late assignments will not be accepted. Please make sure to get something handed in so that some points can be awarded for the assignment, but do keep in mind that, as with all of the other programs, a program that does not compile will still receive an automatic 0.
Note 1: The information for the first Player object will have your name.
Explanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
class Player
{
public:
Player();
Player( char [], int, int, int );
void printPlayer();
void setName( char [] );
void setGoals( int );
void setAssists( int );
void setRating( int );
int getGoals();
int getAssists();
int getRating();
private:
char name[50];
int goals;
int assists;
int rating;
};
Player::Player()
{
name[0] = '';
goals= 0;
assists=0;
rating=0;
}
Player::Player(char player_name[30],int goals,int assists,int rating)
{
strcpy(name,player_name);
this->goals= goals;
this->assists=assists;
this->rating=rating;
}
void Player::setName( char playerName[] )
{
strcpy(name,playerName);
}
void Player::setGoals( int goalsScored )
{
if(goalsScored< 0)
cout <<" setGoals error: goals scored cannot be negative " << endl;
else
this->goals= goalsScored;
}
void Player::setAssists( int assistsEarned )
{
if(assistsEarned<0)
cout << " setAssists error: assists earned cannot be negative " << endl;
else
this->assists=assistsEarned;
}
void Player::setRating( int plusMinusChange )
{
this->rating=plusMinusChange;
}
int Player::getGoals()
{
return goals;
}
int Player::getAssists()
{
return assists;
}
int Player::getRating()
{
return rating;
}
void Player::printPlayer()
{
cout << name << endl;
cout <<noshowpos <<"Goals: " <<goals <<" Assists: "<< assists<<" Points: "
<<(goals+assists) <<" Plus/Minus: " << showpos <<rating << endl;
}
int main()
{
Player player1("Kumar",1,1,+1);
Player player2;
Player player3("Jonathan Toews",10,9,+6);
Player player4("Patrick Kane",11,10,-3);
Player player5("Brandon Saad",5,8,+8);
Player player6("Brandon Pirri",6,5,+6);
cout << endl;
cout<<"The first Player object " << endl;
player1.printPlayer();
cout << endl;
player1.setGoals(player1.getGoals()+2);
player1.printPlayer();
cout << endl;
cout <<"The second Player object" << endl;
player2.printPlayer();
cout << endl;
player2.setName("Patrick Sharp");
player2.setGoals(7);
player2.setAssists(13);
player2.setRating(+10);
player2.printPlayer();
cout << endl;
cout <<"The third Player object " << endl;
player3.printPlayer();
cout << endl;
player3.setGoals(-8);
player3.setAssists(-2);
player3.printPlayer();
cout << endl;
cout <<"The fourth Player object" <<endl;
player4.printPlayer();
cout << endl;
player4.setAssists(player4.getAssists()+3);
player4.setRating(player4.getRating()+3);
player4.printPlayer();
cout << endl;
cout <<"The fifth Player object" << endl;
cout << endl;
cout << "Player 5 has scored "<<noshowpos<< player5.getGoals() << " goals and has a plus/minus rating of " << showpos << player5.getRating() << endl;
player5.printPlayer();
cout << endl;
cout<<"The sixth Player object" << endl;
player6.printPlayer();
cout << endl;
player6.setAssists(-2);
player6.setAssists(player6.getAssists()+4);
cout<<"Player 6 has " <<noshowpos << player6.getAssists() <<" assists " << endl;
player6.setRating(player6.getRating()-4);
player6.printPlayer();
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.