View
237
Download
0
Category
Preview:
DESCRIPTION
Class Method Modifier const class Student { private: string id; string firstName, lastName; float gpa; Public: // The method will NOT change any data members: const. void Write() const { cout gpa; } }; 3
Citation preview
Class Method Readclass Student{private: string id; string firstName, lastName; float gpa;
public:
// Will the method change any data members? // Yes! void Read() { cin >> id >> firstName >> lastName >> gpa; }
};
1
Class Method Writeclass Student{private: string id; string firstName, lastName; float gpa;
public:
// Will the method change any data members? // NO! void Write() { cout << setw(6) << id << setw(15) << firstName << setw(15) << lastName << setw(8) << gpa; }
};2
Class Method Modifier constclass Student{private: string id; string firstName, lastName; float gpa;Public: // The method will NOT change any data members: const. void Write() const { cout << setw(6) << id << setw(15) << firstName << setw(15) << lastName << setw(8) << gpa; } // The method will change data members: no const. void Read() { cin >> id >> firstName >> lastName >> gpa; }}; 3
Class Method Modifier constclass Student{private: string id; string firstName, lastName; float gpa;public: void Write() const
float getGPA() const
string getFirst() const
// No const void Read() void setGPA( float value ) void updateGPA( float amount ) . . .};
4
Comparing Objects
Student s1, s2;
// Input datas1.Read();s2.Read();
//Comparing GPAs of s1 and s2if (s1.getGPA() > s2.getGPA()) cout << s1.getFirst() << “ has higher GPA.”;else if (s1.getGPA() < s2.getGPA()) cout << s2.getFirst() << “ has higher GPA.”;else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst();
5
Comparing GPA without getGPAStudent s1, s2;
// Input datas1.Read();s2.Read();
//Comparing GPAs of s1 and s2if ( ? ) cout << s1.getFirst() << “ has higher GPA.”;else if ( ? ) cout << s2.getFirst() << “ has higher GPA.”;else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst();
Need a New Method!
6
Class Method HasHigherGPAThanclass Student{private: string id; string firstName, lastName; float gpa;public: //float getGPA() const string getFirst() const void Read()
bool HasHigherGPAThan( Student s ) { if ( gpa > s.gpa ) return true; else return false; }};
7
Class Method HasHigherGPAThanclass Student{private: string id; string firstName, lastName; float gpa;public:/* Correct but not good. bool HasHigherGPAThan( Student s ) { if ( gpa > s.gpa ) return true; else return false; }*/ // Much better! bool HasHigherGPAThan( Student s ) { return gpa > s.gpa; }};
8
Comparing GPA without getGPAStudent s1, s2;
// Input datas1.Read();s2.Read();
// Comparing GPAs of s1 and s2if ( s1.HasHigherGPAThan(s2) ) cout << s1.getFirst() << “ has higher GPA.”;else if (s2.HasHigherGPAThan(s1) ) cout << s2.getFirst() << “ has higher GPA.”;else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst();
Don’t know the gpa of s1 or s2!
9
Class Method HasHigherGPAThanclass Student{private: string id; string firstName, lastName; float gpa;public:
// Does it change any data member? // NO! bool HasHigherGPAThan( Student s ) { return gpa > s.gpa; }};
10
Class Method HasHigherGPAThanclass Student{private: string id; string firstName, lastName; float gpa;public:
// Will not change any data member: const bool HasHigherGPAThan( Student s ) const { return gpa > s.gpa; }};
Passing Object as Parameter.
11
Passing Parameters• Basic Data Types (char, int, float, string, bool) Pass by Value (without & in prototype) Pass by Reference (with & in prototype)• Arrays (of any data type) Always Pass by Reference (Never &)• Class Object (and structure) C++ allows pass by value (no &) and by reference (&) Our Rule: Always pass by reference with & Use const for in parameter
12
Passing Object as Parameterclass Student{private: string id; string firstName, lastName; float gpa;
public: // Will not change any data member: const // Will not change parameter s: const with & bool HasHigherGPAThan( const Student& s ) const { return gpa > s.gpa; }};
13
Passing Object as Parameterclass Student{private: string id; string firstName, lastName; float gpa;public:
// Copy constructor: // gets initial values from object s // Will change data members: no const. // Will not change parameter s: const with & Student( const Student& s) { id = s.id; firstName = s.firstName; lastName = s.lastName; gpa = s.gpa; }};
14
Array of Objectsconst int MAX_SIZE = 35;
int numStudents;
// 35 floatsfloat scores[MAX_SIZE];
// 35 StudentsStudent students[MAX_SIZE];
// Single class objectStudent s;
Each array element of students[] is the same as variable s.
15
Array of Objectsconst int MAX_SIZE = 35;
int numStudents;
Student s, students[MAX_SIZE];
// Input datas.Read();students[0].Read();
// Output datas.Write();students[1].Write();
// Update GPAs.UpdateGPA(0.2);students[index].UpdateGPA(0.5);
16
const int MAX_SIZE = 35;
class Student{
};
int main(){ int numStudents; Student s, students[MAX_SIZE];
cin >> numStudents;
for (int i = 0; i < numStudents; i ++) students[i].Read();
. . .
return 0;}
17
Using a function to read data to an array of Student
Function PrototypeFunction Name InputToStudentArray
Function Type void
Parameters s[] : array of Student size: int, number of students of s[]
// Parameters: (Out, Out)void InputToStudentArray(Student s[], int& size);
18
//--------------------------------------------------// The function inputs value of size and then reads // data for size students into array s[].// Parameters: (Out, Out)//--------------------------------------------------void InputToStudentArray(Student s[], int& size){ cin >> size; for (int i = 0; i < size; i++) s[i].Read();
return;}
19
//--------------------------------------------------// The function displays data of size students// of array s[].// Parameters: (In, In)//--------------------------------------------------void PrintStudentArray(const Student s[], int size){ for (int i = 0; i < size; i++) s[i].Write();
return;}
20
//--------------------------------------------------// The function returns the index of the first // array element of s[] that has the same// firstName and lastName as student m.// Parameters: (In, In, In)//--------------------------------------------------int FindStudent(const Student s[], int size, const Student& m){ for (int i = 0; i < size; i++) if (s[i].getFirst() == m.getFirst() && s[i].getLast() == m.getLast()) return i;
return -1;}
21
const int MAX_SIZE = 35;
class Student{
};
void InputToStudentArray(Student s[], int& size);void PrintStudentArray(const Student s[], int size);int FindStudent(const Student s[], int size, const Student& m);
int main(){
. . .
return 0;}
// Function definitions
22
void InputToStudentArray(Student s[], int& size);void PrintStudentArray(const Student s[], int size);int FindStudent(const Student s[], int size, const Student& m);
int main(){ int numStudents, index; Student students[MAX_SIZE];
InputToStudentArray(students, numStudents);
PrintStudentArray(students, numStudents);
Student s(“Qi”, “Yang”, 2.9);
index = FindStudent(students, numStudents, s);
students[index].UpdateGPA(1.0);
PrintStudentArray(students, numStudents);
return 0;}
23
Schedule
• Quiz7-2: Due Wednesday
• Program 5Pairs by Friday
• Lab 9Thursday
24
Recommended