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

Remove the initialize method from the Hockey.h file and replace it with a constr

ID: 674639 • Letter: R

Question


Remove the initialize method from the Hockey.h file and replace it with a constructor that accomplishes the same result. Remove the calls to initialize in the main program. Test it to make sure it works.
Add the following function (and its prototype) to the application file (this is not a member function) to be called from main:

void score_goals (Hockey player, int goals)

{

   int count;

   for (count = 0; count < goals; count++)

     player.score_goal ();

   cout << “ Player in function: “;

   player.print ();

   cout << “ ”;

}

1. Explain in English what the for loop above is accomplishing.

2. What type of parameter passing is used in the function (call by value or call by reference)?

Remove all the previous calls to the public functions in main except for the calls to print at the end. Add a call to score_goals before the calls to print that will score 7 goals for Eichel. Run your program.

3. Explain why Eichel’s statistics are not changed in main.

Change the parameter(s) correctly in the prototype and function header so that the changes made by score_goals will change the player’s statistics in main.

4. Show the new prototype here:

______________________________________________________________________________________

HOCKEY.H

#include
using namespace std;

// Hockey class keeping track of a player's goals, assists,
// penalties and penalty minutes
class Hockey
{
   private:
       int goals, // number of goals
       assists, // number of assists
       penalties, // number of penalties
       penalty_minutes; // total of penalty minutes
      
   public:
       void initialize ();
       void tripping ();
       void fighting ();
       void score_goal ();
       void assist_goal ();
       void print ();
};

// Records the fact that the player has scored another goal
void Hockey::score_goal()
{
   goals++;
}

// Prints the player's current statistics
void Hockey::print()
{
       cout << " Goals: " << goals;
       cout << " Assists: " << assists;
      
   if (penalties == 1)
       cout << " " << penalties << " penalty for " << penalty_minutes << " minutes.";
   else
       cout << " " << penalties << " penalties for " << penalty_minutes << " minutes.";
}

//Initalizes all the values to zero initially
void Hockey::initialize()
{
   goals = 0;
   assists = 0;
   penalties = 0;
   penalty_minutes = 0;
}


// tripping function increases the penalities by one and penalty minutes by 2
void Hockey::tripping()
{
   penalties = penalties+1;
   penalty_minutes = penalty_minutes+2;
}

// fighting function increases the penalities by 1 and penalty minutes by 5
void Hockey::fighting()
{
   penalties = penalties+1;
   penalty_minutes = penalty_minutes+5;
}

//assist_goal updates the assist value by 1
void Hockey::assist_goal()
{
   assists = assists+1;
}

__________________________________________________________________________________________

HOCKEY MAIN ()

// Main program that uses the Hockey class
#include
#include

using namespace std;

#include "Hockey.h"

int main(int argc, char *argv[])
{
   Hockey eichel, kane;
  
    cout << "Initalizing the values of Eichel and Kane";
       eichel.initialize();
        kane.initialize();
        cout << endl;
       
   cout << "Updating Eichel's stats";
       eichel.score_goal();
       eichel.score_goal();
       eichel.assist_goal();
        eichel.tripping();
        cout << endl;
       
    cout << "Updating Kane's stats";
   
        kane.assist_goal();
       kane.assist_goal();
        kane.assist_goal();
        kane.fighting();
       kane.fighting();
       cout << endl;
       cout << endl;
   cout << "Eichel stats:";
    eichel.print();
   
   cout << " Kane stats: ";
        kane.print();
   
   cout << " ";
  
   system("PAUSE");
   return EXIT_SUCCESS;
}

Explanation / Answer

void score_goals (Hockey player, int goals)

{

   int count;

   for (count = 0; count < goals; count++)

     player.score_goal ();

   cout << “ Player in function: “;

   player.print ();

   cout << “ ”;

}

Sample output:

Modified program:

/**hockey.h**/

#include <iostream>

using namespace std;

// Hockey class keeping track of a player's goals, assists,

// penalties and penalty minutes

class Hockey

{

private:

int goals, // number of goals

assists, // number of assists

penalties, // number of penalties

penalty_minutes; // total of penalty minutes

public:

Hockey();

void tripping ();

void fighting ();

void score_goal ();

void assist_goal ();

void print ();

};

//constructor that initializes the class variables

// Records the fact that the player has scored another goal

Hockey::Hockey()

{

goals = 0;

assists = 0;

penalties = 0;

penalty_minutes = 0;

}

// Records the fact that the player has scored another goal

void Hockey::score_goal()

{

goals++;

}

// Prints the player's current statistics

void Hockey::print()

{

cout << " Goals: " << goals;

cout << " Assists: " << assists;

if (penalties == 1)

cout << " " << penalties << " penalty for " << penalty_minutes << " minutes.";

else

cout << " " << penalties << " penalties for " << penalty_minutes << " minutes.";

}

// tripping function increases the penalities by one and penalty minutes by 2

void Hockey::tripping()

{

penalties = penalties+1;

penalty_minutes = penalty_minutes+2;

}

// fighting function increases the penalities by 1 and penalty minutes by 5

void Hockey::fighting()

{

penalties = penalties+1;

penalty_minutes = penalty_minutes+5;

}

//assist_goal updates the assist value by 1

void Hockey::assist_goal()

{

assists = assists+1;

}

/**main**/

#include <iostream>

#include "Hockey.h"

using namespace std;

void score_goals(Hockey ,int);

int main(int argc, char *argv[])

{

Hockey eichel, kane;

cout << "Initalizing the values of Eichel and Kane";

cout << endl;

cout << "Updating Eichel's stats...";

eichel.score_goal(); //goal=0+1=1

eichel.score_goal(); //goal =1+1=2

eichel.assist_goal();

eichel.tripping();

cout << endl;

cout << "Updating Kane's stats";

kane.assist_goal();

kane.assist_goal();

kane.assist_goal();

kane.fighting();

kane.fighting();

cout << endl;

cout << endl;

cout << "Eichel stats:";

eichel.print();

cout << "agian Updating Eichel's stats";

score_goals(eichel,3); // goal= 2+3=5

cout << " Kane stats: ";

kane.print();

cout << " ";

cout << "agian Updating Kane stats";

score_goals(kane,4); // goal= 0+4=4

system("PAUSE");

return EXIT_SUCCESS;

}

//adds number of goals the player(Note: score_goal() adds only one , but score_golas may add more than one goal)

void score_goals (Hockey player, int goals)

{

int count;

//calls score_goal 'goals' times to add goals to player score

for (count = 0; count < goals; count++)

player.score_goal (); //for each call socre incresed by 1

cout << " Player in function: ";

player.print ();//finally prints the player statistics

cout << " ";

}