Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) On lines 29 to 33 of main.cpp, why is there an ‘&’ before the player variable

ID: 3870805 • Letter: 1

Question

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

Football project file

main.cpp

/*

* This is the main class for CSC 4111 Lab 1 Task 2

* You are not allowed to change this file.

* 10 points will be subtracted if this file is modified.

*

* @Author C. Dorman

*/

#include <iostream>

#include <string>

#include "player.h"

#include "offense.h"

#include "defense.h"

#include <windows.h>

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");

}

Player.cpp

/*

* This is the implementation file for the player class for CSC 4111 Lab 1 Task 2

* You are not allowed to change this file.

* 10 points will be subtracted if this file is modified.

*

* @Author C. Dorman

*/

#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 << " Number: " << number;

}

Thank You

Task 2 - 30 points We need to write a program to help our favorite team keep track of its players' performance. The file that will print out the player's 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 team's stats, so it must follow the guidelines below. You may not change the files given in any way Download the Football project and unzip it. It does not compile because it misses 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).

Explanation / Answer

Below is your working program: -

defense.cpp

/*

* This is the implementation file for the defensive player class for CSC 4111 Lab 1 Task 2

* It is part of the solution to Lab 1

*

* @Author C. Dorman

*/

#include "defense.h"

/*

* This is the constructor for the defensive class.

* All it does is call the base constructor.

* @param The player's name.

*/

Defense::Defense(string name) : Player(name){

}

/*

* This is the implementation for the pure virtual method.

* It sets the defensive player's minutes. It should be in the base class,

* because there is nothing specific to an defensive player.

* @param the number of minutes the player was in the game.

*/

void Defense::setMinutesPlayed(int minutes){

this->minutes = minutes;

}

/*

* This method sets the defensive player's tackles.

* @param the number of yards the player had.

*/

void Defense::setTackles(int tackles){

this->tackles = tackles;

}

/*

* This is a method to print out a players stats, it overrides, but then calls

* the base class method to print the private attribute name.

*/

void Defense::printStats() const{

Player::printStats();

cout << " Minutes: " << minutes << " Tackles: " << tackles << endl;

}

main.cpp

/*

* This is the main class for CSC 4111 Lab 1 Task 2

* You are not allowed to change this file.

* 10 points will be subtracted if this file is modified.

*

* @Author C. Dorman

*/

#include <iostream>

#include <string>

#include "player.h"

#include "offense.h"

#include "defense.h"

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();

}

offense.cpp

/*

* This is the implementation file for the offensive player class for CSC 4111 Lab 1 Task 2

* It is part of the solution to Lab 1

*

* @Author C. Dorman

*/

#include "Offense.h"

/*

* This is the constructor for the offensive class.

* All it does is call the base constructor.

* @param The player's name.

*/

Offense::Offense(string name) : Player(name){

}

/*

* This is the implementation for the pure virtual method.

* It sets the offensive player's minutes. It should be in the base class,

* because there is nothing specific to an offensive player.

* @param the number of minutes the player was in the game.

*/

void Offense::setMinutesPlayed(int minutes){

this->minutes = minutes;

}

/*

* This method sets the offensive player's yards.

* @param the number of yards the player had.

*/

void Offense::setYards(int yards){

this->yards = yards;

}

/*

* This is a method to print out a players stats, it overrides, but then calls

* the base class method to print the private attribute name.

*/

void Offense::printStats() const{

Player::printStats();

cout << " Minutes: " << minutes << " Yards: " << yards << endl;

}

offense.h

/*

* This is the header file for the offensive player class for CSC 4111 Lab 1 Task 2

* It is part of the solution to Lab 1

*

* @Author C. Dorman

*/

#ifndef OFFENSE_H

#define OFFENSE_H

#include "player.h"

using namespace std;

class Offense : public Player{

public:

/*

* This is the constructor for the offensive class.

* All it does is call the base constructor.

* @param The player's name.

*/

Offense(string name);

/*

* This is the implementation for the pure virtual method.

* It sets the offensive player's minutes. It should be in the base class,

* because there is nothing specific to an offensive player.

* @param the number of minutes the player was in the game.

*/

virtual void setMinutesPlayed(int minutes);

/*

* This method sets the offensive player's yards.

* @param the number of yards the player had.

*/

void setYards(int yards);

/*

* This is a method to print out a players stats, it overrides, but then calls

* the base class method to print the private attribute name.

*/

virtual void printStats() const;

private:

/* The number of yards the player had */

int yards;

};

#endif

player.cpp

/*

* This is the implementation file for the player class for CSC 4111 Lab 1 Task 2

* You are not allowed to change this file.

* 10 points will be subtracted if this file is modified.

*

* @Author C. Dorman

*/

#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 << " Number: " << number;

}

player.h

/*

* This is the header file for the player class for CSC 4111 Lab 1 Task 2

* You are not allowed to change this file.

* 10 points will be subtracted if this file is modified.

*

* @Author C. Dorman

*/

// Question 3 - What do the #ifndef, #define and #endif preprocessor commands do in this file?

#ifndef PLAYER_H

#define PLAYER_H

#include <iostream>

#include <string>

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

defense.h

/*

* This is the header file for the defensive player class for CSC 4111 Lab 1 Task 2

* It is part of the solution to Lab 1

*

* @Author C. Dorman

*/

#ifndef DEFENSE_H

#define DEFENSE_H

#include "player.h"

using namespace std;

class Defense : public Player{

public:

/*

* This is the constructor for the defensive class.

* All it does is call the base constructor.

* @param The player's name.

*/

Defense(string name);

/*

* This is the implementation for the pure virtual method.

* It sets the defensive player's minutes. It should be in the base class,

* because there is nothing specific to an defensive player.

* @param the number of minutes the player was in the game.

*/

virtual void setMinutesPlayed(int minutes);

/*

* This method sets the defensive player's tackles.

* @param the number of yards the player had.

*/

void setTackles(int tackles);

/*

* This is a method to print out a players stats, it overrides, but then calls

* the base class method to print the private attribute name.

*/

virtual void printStats() const;

private:

/* The number of tackles the player had */

int tackles;

};

#endif

Answers of your Questions: -

1. Because team is array of pointers so , we need to store address of each player object, so we have used &.

2. Because seeting values in object inside array of pointers is combursome and not a good coding practice.

3. #ifndef check whether the the file and code between endif is included/defined or not to protect it from redefining and #define helps to define the code.

4. name is private , so we cannot use it outside class, so we have to restrict its usage within the class.

5. Because minute and number is to be needed in offense and defense classes, as these classes are child classes of player class, we made it protected.

6. Adding =0 makes it pure virtual function which means that object of class cannot be instantiated without defining it.

7. Const is used in the function declaration to restrict the function from modifying the object, thus making the onject for function read only.