Download presentation
Presentation is loading. Please wait.
Published byRoderick Williams Modified over 9 years ago
1
Class Method Read class 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
2
Class Method Write class 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
3
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 << 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
4
Class Method Modifier const class 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
5
Comparing Objects Student s1, s2; // Input data s1.Read(); s2.Read(); //Comparing GPAs of s1 and s2 if (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
6
Comparing GPA without getGPA Student s1, s2; // Input data s1.Read(); s2.Read(); //Comparing GPAs of s1 and s2 if ( ? ) 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
7
Class Method HasHigherGPAThan class 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
8
Class Method HasHigherGPAThan class 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
9
Comparing GPA without getGPA Student s1, s2; // Input data s1.Read(); s2.Read(); // Comparing GPAs of s1 and s2 if ( 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
10
Class Method HasHigherGPAThan class 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
11
Class Method HasHigherGPAThan class 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
12
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
13
Passing Object as Parameter class 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
14
Passing Object as Parameter class 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
15
Array of Objects const int MAX_SIZE = 35; int numStudents; // 35 floats float scores[MAX_SIZE]; // 35 Students Student students[MAX_SIZE]; // Single class object Student s; Each array element of students[] is the same as variable s. 15
16
Array of Objects const int MAX_SIZE = 35; int numStudents; Student s, students[MAX_SIZE]; // Input data s.Read(); students[0].Read(); // Output data s.Write(); students[1].Write(); // Update GPA s.UpdateGPA(0.2); students[index].UpdateGPA(0.5); 16
17
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
18
Using a function to read data to an array of Student Function Prototype Function 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
19
//-------------------------------------------------- // 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
20
//-------------------------------------------------- // 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
21
//-------------------------------------------------- // 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
22
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
23
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
24
Schedule Quiz7-2: Due Wednesday Program 5 Pairs by Friday Lab 9 Thursday 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.