Question
We need to write a program to help our favorite team keep track of its players performance. The file that will print out the players
We need to write a program to help our favorite team keep track of its players performance. The file that will print out the players stats is done. You are given a class for a player; however, a football team is composed of offensive and defensive players. So you need to build on the player class. It is rare that a player is both, so we will assume our team has exclusively offensive and defensive players. All players have a name, number and they all play for some number of minutes. However, a defensive player is measured by the number of tackles they get, while an offensive player is measured by the number of yards they get. For this assignment you need to create 2 classes that inherit from a class player that has already been written. This class makes no calculations; it just holds data on each player. Your class will be used by the main function in main.cpp to print the teams stats, so it must follow the guidelines below. You may not change the files given in any way.
.................................................................................................................................................................................
Given Files:
player.h
/* * This is the header file for the player class * You are not allowed to change this file. * 10 points will be subtracted if this file is modified. * */
// Question 3 - What do the #ifndef, #define and #endif preprocessor commands do in this file? #ifndef PLAYER_H #define PLAYER_H
#include #include
using namespace std;
class Player{ private: string name; // The player's name, notice it's private, Question 4 - What does this mean you have to do?
protected: // Question 5 - Why does number have to be private. int number; // The player's number. int minutes; // The number of minutes the player is in the game.
public:
/* * This is the constructor for the player class. * @param The player's name. */ Player(string name);
/* * This is a mutator method, it sets the player's number. * @param The player's number. */ void setNumber(int number);
/* * Add comments for this method in your inherited class. */ virtual void setMinutesPlayed(int minutes) = 0; // Question 6 - why does this method equal zero? /* * This is a method to print out a players stats, its virtual, * so it is possible to override it. */ virtual void printStats() const; // Question 7 - What does 'const' do here? };
#endif
.................................................................................................................................................................................
player.cpp
/* * This is the implementation file for the player class * You are not allowed to change this file. * 10 points will be subtracted if this file is modified. * */
#include "player.h"
/* * This is the constructor for the player class. * @param The player's name. */ Player::Player(string name){ this->name = name; }
/* * This is a mutator method, it sets the player's number. * @param The player's number. */ void Player::setNumber(int number){ this->number = number; }
/* * This is a method to print out a players stats, its virtual, * so it is possible to override it. */ void Player::printStats() const{ cout << "Name: " << name << "\tNumber: " << number; }
.................................................................................................................................................................................
main.cpp
/* * This is the main class * You are not allowed to change this file. * 10 points will be subtracted if this file is modified. * */
#include #include #include "player.h" #include "offense.h" #include "defense.h" #include using namespace std;
int main(){
// Create the players for the team. Offense player("Bob"); Defense player1("Jane"); Offense player2("Sai"); Defense player3("Chin"); Offense player4("Kali");
Player *team[5]; // Make a team of pointers to players.
// Question 1 - Why is there an '&' before the players on lines 29 to 33? team[0] = &player; team[1] = &player1; team[2] = &player2; team[3] = &player3; team[4] = &player4;
// Set the player numbers for(int i=0; i<5; i++) team[i]->setNumber(i+10);
// Set the player minutes. for(int i=0; i<5; i++) team[i]->setMinutesPlayed(i*7+(i+1)*i+1);
// Question 2 - Why not use a loop for lines 45 to 49? // Set the player's stats. player.setYards(34); player1.setTackles(5); player2.setYards(23); player3.setTackles(7); player4.setYards(132);
// Print out each player's stats. for(int i=0; i<5; i++) team[i]->printStats();
system("pause");
}
The program does not compile because it's missing some classes. The project needs 4 files added: defense.h, defense.cpp, offense.h, offense.cpp (10 points). There are also a few questions that must be answered (20 points).
.
Note: the main.cpp, player.h and player.cpp files must not be changed in any way. The added files should be formatted similarly to the Player class files. The classes need to have the following, and you must decide the return types, visibility and any additional modifiers for these methods:
.
defense class fields:
int tackles;
defense class methods:
Defense(string name);
setMinutesPlayed(int minutes);
setTackles(int tackles);
printStats() const;
.
offense class fields:
int yards;
offense class methods:
Offense(string name);
setMinutesPlayed(int minutes);
setYards(int yards);
printStats() const;
.
Rules:
Your classes must inherit from the base class Player.
You should not recode anything. If the base class already does something, do NOT put it in your derived class. Use what has been already there.
You should follow the coding conventions of the program given.
All files should have a brief description at the beginning that includes the title of the file and your name. The code should have comments describing each method and large section. See the provided code for examples.
All 4 of the methods listed for each new class above must be implemented. You can add extra methods, but it is not necessary and there is no bonus.
This is just a suggestion: the solution averages exactly one line of code (LOC) inside each method, so if a method has 2 LOC, another should have 0 LOC. There is no trick here either with crazy lines that call multiple methods or anything else. Basically, if you are writing many lines of code per method, you need to review inheritance or polymorphism.
All classes should include all needed libraries.
.
Questions:
1) On lines 29 to 33 of main.cpp, why is there an & before the player variables?
2) For lines 45 to 49 of main.cpp, why not use a loop?
3) On lines 10, 11 and 52 of player.h, what do the #ifndef, #define and #endif preprocessor commands do?
4) On line 20 of player.h, the string name is private, what did you have to do because of this?
5) On lines 24 of player.h, why does minutes played have to be protected, not private?
6) On line 43 of player.h, why is the function assigned zero?
7) On line 49 of player.h, what does the word const do?
8) What method should be entirely in the base class? Why?
9) Which class is an abstract class? Why?
10) Name one thing you learned or had forgotten and remembered in this task.
.
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started