Hockey.cpp #include #include using namespace std; #include \"Hockey.h\" int main
ID: 3862034 • Letter: H
Question
Hockey.cpp
#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 << " Updating Eichel's stats";
eichel.score_goal();
eichel.score_goal();
eichel.assist_goal();
eichel.tripping();
cout << " Updating Kane's stats";
kane.assist_goal();
kane.assist_goal();
kane.assist_goal();
kane.fighting();
kane.fighting();
cout << " Eichel stats:";
eichel.print();
cout << " Kane stats: ";
kane.print();
cout << " ";
system("PAUSE");
return EXIT_SUCCESS;
}
Hockey.h
#pragma once
#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;
}
Please answer these questions and update the code
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. If the value 7 is passed in for the parameter goals, what does this function accomplish? Don’t give a step by step description instead give the big picture of what it does.
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 Eichel’s statistics in main.
4. Show the new prototype here:
Explanation / Answer
1) If the value 7 is passed for goals parameter, then it increases the number of goals 7 times and prints goals for Eichel as 7. But Eichel's statistics don't change (Goals: 0).
2) The parameter passing is of type call by value.
3) Eichel's statistics aren't changed in main becase the parameter is called by value. Only the value of the variable eichel is copied into a variable used by the method. The object eichel is not changed. Only the variable that the function uses changes its value.
4) The new prototype with call by reference is:
Here is the program with the required changes:
Hockey.h:
Hockey.cpp:
void score_goals(Hockey *player, int goals);Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.