CS 1430: Programming in C++
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) };
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; Student s3(first, last, myGPA);
Creating Class Objects // Creating a Student object by calling a 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; s = Student(first, last, myGPA);
Array of Objects int numStudents; string id, first, last; float myGPA; // Must have default constructor Student s, allStudents[30]; cin >> id >> last >> first >> myGPA; // Creating an object and assign it to s s = Student(id, first, last, myGPA); // Copy s to allStudents[0] allStudents[0] = s;
Array of Objects int numStudents; string id, first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> id >> last >> first >> myGPA; s = Student(id, first, last, myGPA); allStudents[i] = s; }
Array of Objects int numStudents; string id, first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> id >> last >> first >> myGPA; //s = Student(id, first, last, myGPA); allStudents[i] = Student(id, first, last, myGPA); }
Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: void Read() string id, first, last; float myGPA; cin >> numStudents; for (int i = 0; i < numStudents; i ++) cin >> id >> last >> first >> myGPA; allStudents[i] = Student(id, first, last, myGPA); } };
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 };
Class Student and StudentList { private: . . . public: void Read() void Write() const }; class StudentList
Class Student and StudentList { . . . }; class StudentList int main() StudentList cs143_S2; cs143_S2.Read(); cs143_S2.Write(); return 0; }
More Methods for Class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: . . . float MaxGPA() const float max = students[0].GetGPA(); for (int i = 1; i < numStudents; i ++) if (students[i].GetGPA() > max) max = students[i].GetGPA(); return max; } };
Class Student and StudentList { }; 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; }
More Methods for 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; };
Class Student and StudentList 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; }
More Methods for 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]; } };
Class Student and StudentList { }; 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; }
More Methods for 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[] };
More Methods for 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; } };
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() << ‘.’; } else cout << endl << s.GetFirst() << “ ” << s.GetLast() << “ is not in CS143 Section 2.”; return 0;
Schedule Quiz7-4 Lab 9 Program 5 Pairs Sign up Today