9
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

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

Embed Size (px)

Citation preview

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

Class Scopeclass 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

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

Class Tournamentclass 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

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

Without Class Variableclass 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

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

Class Tournamentclass 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

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

Class Tournamentclass 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

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

Class Tournamentclass 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

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

Class PlayerListclass 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

}

}; // PlayerList7

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

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

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

Schedule

• Lab 9: Grace today

• Program 5

Due Wednesday, April 20

Grace Tuesday, April 26

• Thursday

Do Program 5

9