I'm doing a class program and the output shows some garbage values so I figured some things needed to be call by reference but I'm not sure as to where. Sorry if the code doesn't display nicely, I haven't mastered how to display it on here yet.
header
#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++;
}
Hello dear cplusplus annoyed,
the problom with the code is that you never actually initizalized the private member variables of the hockey class. You defined a function 'void initialize()' but you never called it in main once you defined the 'eichel' and 'kane' objects.
Try adding the lines
1 2
eichel.initialize();
kane.initialize();
Otherwise if you do not wan to worry about doing this each time you create a hockey object in the header file instead of 'void initialize()' write:
class Hockey{
private:
int goals, // number of goals
assists, // number of assists
penalties, // number of penalties
penalty_minutes; // total of penalty minutes
public:
Hockey (); // Constructors: takes care of initializing variables
void tripping ();
void fighting ();
void score_goal ();
void assist_goal ();
void print ();
};
Hockey::Hockey (){
goals=0;
assists=0;
penalties=0;
penalty_minutes=0;
}