Download presentation
Presentation is loading. Please wait.
1
CS 1430: Programming in C++
2
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; } };
3
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; } };
4
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; };
5
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 ) . . . };
6
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();
7
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!
8
Class Method HasHigherGPAThan
class Student { private: string id; string firstName, lastName; float gpa; public: float getGPA() const string getFirst() const void Read() // Parameter is of class Student bool HasHigherGPAThan( Student s ) if ( gpa > s.gpa ) return true; else return false; } };
9
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! return gpa > s.gpa; };
10
Comparing GPA without Knowing GPA
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!
11
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; } };
12
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.
13
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
14
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; } };
15
Each array element of students[] is the same as variable s.
Array of Objects const int MAX_SIZE = 35; int numStudents; // parallel array to keep students’ data float scores[MAX_SIZE]; string firstNames[MAX_SIZE]; // 35 Students Student students[MAX_SIZE]; // Single class object Student s; Each array element of students[] is the same as variable s.
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);
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; }
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);
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; }
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; }
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; }
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
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); return 0; }
24
Starting Tuesday evening
Schedule Quiz7-2 Due today by 10 PM Quiz7-4 Due Wednesday by 10 PM Lab 8 3 points by 5 PM, Tuesday Lab 9 Starting Tuesday evening
25
Pair Signup by Wednesday
Schedule Prog4 Grace Time: 10 PM, Tuesday Prog5 Must do it in pairs! Pair Signup by Wednesday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.