Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.

Similar presentations


Presentation on theme: "Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string."— Presentation transcript:

1 Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) Student(const Student& s) }; 1

2 Creating Class Objects // Must have the default constructor Student s; Student s1(); // Creating a Student object by calling // Student(string first, string last, float g) Student s2(“Qi”, “Yang”, 2.9); string first, last; float myGPA; cin >> last >> first >> myGPA; // Creating a Student object by calling // Student(string first, string last, float g) Student s3(first, last, myGPA); 2

3 Creating Class Objects // Works even without default constructor Student s = Student(“Qi”, “Yang”); // Override data of s s.Read(); // Creating an object and assign it to s s = Student(“13579”, “Joe”, “Clifton”, 3.9); string first, last; float myGPA; cin >> last >> first >> myGPA; // Creating an object and assign it to s s = Student(first, last, myGPA); 3

4 Array of Objects int numStudents; string first, last; float myGPA; // Must have default constructor Student s, allStudents[30]; cin >> last >> first >> myGPA; // Creating an object and assign it to s s = Student(first, last, myGPA); // Copy s to allStudents[0] allStudents[0] = s; 4

5 Array of Objects int numStudents; string first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> last >> first >> myGPA; s = Student(first, last, myGPA); allStudents[i] = s; } 5

6 Array of Objects int numStudents; string first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> last >> first >> myGPA; //s = Student(first, last, myGPA); allStudents[i] = Student(first, last, myGPA); } 6

7 Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: void Read() { string first, last; float myGPA; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> last >> first >> myGPA; allStudents[i] = Student(first, last, myGPA); } } }; 7

8 Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: void Read() { cin >> numStudents; for (int i = 0; i < numStudents; i ++) // Assuming class Student has method Read() allStudents[i].Read(); } void Write() const { // Print all students one by one } }; 8

9 Class Student and StudentList class Student { private:... public: void Read() void Write() const }; class StudentList { private:... public: void Read() void Write() const }; 9

10 Class Student and StudentList class Student {... }; class StudentList {... }; int main() { StudentList CS143_S2; CS143_S2.Read(); CS143_S2.Write();... return 0; } 10

11 More Methods for Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public:... float MaxGPA() const { float max = students[0]; for (int i = 1; i < numStudents; i ++) if (students[i].GetGPA() > max) max = students[i].GetGPA(); return max; } }; 11

12 Class Student and StudentList class Student { }; class StudentList { }; int main() { StudentList CS143_S2; float maxGPA; CS143_S2.Read(); CS143_S2.Write(); maxGPA = CS143_S2.MaxGPA(); cout << “The max GPA for CS143 Section 2 is ” << maxGPA; return 0; } 12

13 More Methods for Class StudentList class StudentList { public: float MaxGPA() const float MinGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const { float gpa; max = min = avg = students[0].GetGPA(); for (int i = 1; i < numStudents; i ++) { gpa = students[i].GetGPA(); if (gpa > max) max = gpa; if (gpa < min) min = gpa; avg += gpa; } avg /= numStudents; } }; 13

14 Class Student and StudentList class Student class StudentList int main() { StudentList CS143_S2; float maxGPA, minGPA, avgGPA; CS143_S2.Read(); CS143_S2.Write(); CS143_S2.GetStats(maxGPA, minGPA, avgGPA); cout << “CS143 Section 2” << endl; cout << “ Max GPA:” << maxGPA << endl << “ Min GPA:” << minGPA << endl << “ Avg GPA:” << avgGPA; return 0; } 14

15 More Methods for Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public:... Student MaxGPAStudent() const { int index = 0; for (int i = 1; i < numStudents; i ++) if (students[i].GetGPA() > students[index].GetGPA()) index = i; return students[index]; } }; 15

16 Class Student and StudentList class Student { }; class StudentList { }; int main() { StudentList CS143_S2; Student s; CS143_S2.Read(); CS143_S2.Write(); s = CS143_S2.MaxGPAStudent(); cout << “The student with max GPA for CS143 Section 2 is\n”; s.Write(); return 0; } 16

17 More Methods for Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const { for (int i = 0; i < numStudents; i++) if (students[i].GetFirst() == s.GetFirst() && students[i].GetLast() == s.GetLast()) return i; return -1; } public:... void UpdateStudentGPA(const Student& s, float g) { // Need to find where s is in students[] } }; 17

18 More Methods for Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const public:... bool UpdateStudentGPA(const Student& s, float g) { int index = find(s); if (index == -1) return false; students[index].UpdateGPA(g); return true; } }; 18

19 Class Student and StudentList int main() { StudentList CS143_S2; Student s; CS143_S2.Read(); CS143_S2.Write(); s = Student(“Qi”, “Yang”); if (CS143_S2.UpdateStudentGPA(s, 0.5)) { cout << endl << “GPA is updated for ” << s.GetFirst() << “ ” << s.GetLast() << ‘.’; CS143_S2.Write(); } else cout << endl << s.GetFirst() << “ ” << s.GetLast() << “ is not in CS143 Section 2.”; return 0; } 19

20 Schedule Lab 9: Grace Next Wednesday Program 5 Pairs Today 20


Download ppt "Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string."

Similar presentations


Ads by Google