Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;

Similar presentations


Presentation on theme: "Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;"— Presentation transcript:

1 Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa; } void Print() { cout << endl; cout << setw(9) << id << setw(20) << firstName << setw(20) << lastName << setw(5) << gpa; } }; 1

2 Class Tournament class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; char command; // Class Scope! void processOneCommand() { switch ( command ) { case 'A': // inputs then calls add method break;... } } public: void processAllTransactions() { cin >> command; while ( !cin.eof() ) { processOneCommand(); cin >> command; } } }; // Tournament 2

3 Without Class Variable class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; // char command; void processOneCommand(char command) { switch ( command ) { case 'A': // inputs then calls add method break;... } } public: void processAllTransactions() { char command; cin >> command; while ( !cin.eof() ) { processOneCommand(command); cin >> command; } } }; // Tournament 3

4 Class Tournament class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; Player p1, p2; // Class Scope! void processOneCommand() { switch ( command ) { case 'A': // inputs then calls add method p1.read();... break; case 'D': p1.readName();... break; case 'P': p1.readName(); // p1 is the winner p2.readName(); // p2 is the loser... break; } } public:... }; // Tournament 4

5 Class Tournament class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; Player p1, p2; // Class Scope! int addingResult; // Class Scope! void processOneCommand() { switch ( command ) { case 'A': p1.read(); addingResult = allPlayers.add( p1 ); // Must pass p1 displayAddingMessage(); // Better do it here! break;... break; } } void displayAddingMessage() const { // Access p1 and addingResult } public:... }; // Tournament 5

6 Class Tournament class Tournament { private: RatingAdjustmentLevel chart[MAX_LEVELS]; PlayerList allPlayers; Player p1, p2; // Class Scope! int adjAmt; // Class Scope! void processOneCommand() { switch ( command ) {... case 'P': case 'p': p1.readName(); p2.readName(); // Before call: Do we know adjAmt and rating for p1 and p2? allPlayers.processMatchResult( chart, p1, p2, adjAmt ); // After call: Do we know adjAmt and rating for p1 and p2? displayMatchResultMessage(); break; } } public:... }; // Tournament 6

7 Class PlayerList class PlayerList { private: Player list[MAX_PLAYERS]; int numPlayers; public: // No const for p1 and p2! void processMatchResult( const RatingAdjustmentLevel chart[], Player& p1, Player& p2, int& adjAmt ) { int index1 = find( p1 ); int index2 = find( p2 ); p1 = list[index1]; p2 = list[index2]; // Copy operation // Figure out adjAmt and update p1 and p2 list[index1] = p1; list[index2] = p2; // Copy operation } }; // PlayerList 7

8 Programming Grand Rule Each function / method including the main function can have at most 30 lines! We don’t count the starting and ending braces, but we do count all lines within the braces, including comments and blank lines! 8

9 Schedule Lab 9: Grace today Program 5 Due Wednesday, April 20 Grace Tuesday, April 26 Thursday Do Program 5 9


Download ppt "Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;"

Similar presentations


Ads by Google