// player.cpp -- implementation of the player class // Each object of this class represents a football player. #include "player.h" #include "boolean.h" #include #include #include #include // Default constructor player::player() { strcpy (last_name, ""); strcpy (first_name, ""); number = 0; feet = 0; inches = 0; pounds = 0; birth_day = 0; birth_month = 0; birth_year = 0; } // Initial value constructor assumes that we have a string // parameter that conforms to this example: // Lloyd, Matthew 18 6-3 198 16/4/1978 player::player(char s[]) { char player_number[5], player_height[5], player_weight[5], player_day[5], player_month[5], player_year[5]; // Use strtok() to parse the string into individual tokens. strcpy (last_name, strtok (s, ",")); strcpy (first_name, strtok (0, " ")); strcpy (player_number, strtok (0, " ")); strcpy (player_height, strtok (0, " ")); strcpy (player_weight, strtok (0, " ")); strcpy (player_day, strtok (0, "/ ")); strcpy (player_month, strtok (0, "/")); strcpy (player_year, strtok (0, "/")); // Use atoi() to convert a string to an integer. // Except, in the case of determining the "feet", we only need to // look at one character. number = atoi (player_number); feet = player_height[0] - '0'; inches = atoi (player_height + 2); pounds = atoi (player_weight); birth_day = atoi (player_day); birth_month = atoi (player_month); birth_year = atoi (player_year); } boolean player::over200() { if (pounds >= 200) return TRUE; else return FALSE; } // Assignment operator simply copies all the data attributes. player& player::operator = (const player &p) { strcpy (first_name, p.first_name); strcpy (last_name, p.last_name); number = p.number; feet = p.feet; inches = p.inches; pounds = p.pounds; birth_day = p.birth_day; birth_month = p.birth_month; birth_year = p.birth_year; return *this; } // This is the output operator. We have a lot of formatting // to do in order to make the output look good. // In particular, the name is left justified, while all the // numbers are right justified. We use the ' and " symbols // to signify feet and inches. To make the birthdate column // look vertically aligned, we need to set aside 2 spaces for // the day and month. ostream& operator << (ostream &os, player &p) { char name[30]; strcpy (name, p.first_name); strcat (name, " "); strcat (name, p.last_name); os << "# " << setw(2) << p.number << " "; os << setiosflags(ios::left) << setw(20) << name; os << setiosflags(ios::right); os << setw(2) << p.feet << "' "; os << setw(2) << p.inches << "\""; os << setw(5) << p.pounds << " lbs."; os << setw(7) << "born "; os << setw(2) << p.birth_day << "/"; os << setw(2) << p.birth_month << "/"; os << p.birth_year << endl; return os; }