There will be two sets of files: Hockey.h and Hockeyapp.cpp Hockey.h // Hockey c
ID: 3851570 • Letter: T
Question
There will be two sets of files: Hockey.h and Hockeyapp.cpp
Hockey.h
// 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.";
}
Hockeyapp.cpp
- The public class methods score_goal and print have been defined for you. Add the other public class methods. initialize sets all private data items equal to zero. tripping increases the number of penalties by 1 and increases the penalty minutes by 2. fighting increases the number of penalties by 1 and increases the penalty minutes by 5. assist_goal increases the number of assists by 1.
- In the main program, declare two variables of type Hockey, eichel and kane. Apply the public method initialize to both variables. Use the public methods to have eichel score two goals, get one assist and a penalty for tripping. Use the public methods to have kane get three assists and two penalties for fighting. Use the public method print to display each player’s statistics (after each label printing the player’s name). Get your program to run correctly.
Submit your completed program (Hockeyapp.cpp and Hockey.h files) on Blackboard.
1. Add the following statement to main to print the number of goals that kane has scored:
cout << kane.goals;
Rebuild All from the Execute menu. Why doesn’t this code compile?
Remove this cout.
2. Remove Hockey:: from the score_goal method definition in the Hockey.h file. Select Rebuild All from the Execute menu.
a. What compiler error is now given?
b. What does the Hockey:: indicate to the compiler that allows the code to compile?
Restore the Hockey::
3. Comment out the calls to the initialize method in main (put // before them). Run your program. What happens and why?
Uncomment the code.
4. Comment out the #include “Hockey.h” line. What happens and why?
Explanation / Answer
// Main program that uses the Hockey class
#include <cstdlib>
#include <iostream>
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:";
cout <<eichel.print();
cout << " Kane stats: ";
cout <<kane.print();
cout << " ";
system("PAUSE");
return EXIT_SUCCESS;
}
#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:
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;
}
cout << kane.goals;
Answer:This will not compile since we are directly calling the variable goal from the main fucnction
a. What compiler error is now given?
Answer:'Undefined reference to class::function()'[duplicate]
b. What does the Hockey:: indicate to the compiler that allows the code to compile?
Answer:Indicates that the classname(Hockey) can also be used to access variables and functions from another scope (actually class, struct, namespace are scopes in that case)
Restore the Hockey::
3. Comment out the calls to the initialize method in main (put // before them). Run your program. What happens and why?
Answer:
The variable values goals,assist etc will be initlaized to some values in the previous run and if you don't use the initalize method to reinitalize the value to 0 it uses the junk values and doesn't provide appropriate values.
Uncomment the code.
4. Comment out the #include “Hockey.h” line. What happens and why?
Answer:Throws an error since we are providing a reference to use the varaibles,method names and the file consisting these are not included in the main function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.