Download presentation
Presentation is loading. Please wait.
1
CS 1430: Programming in C++
2
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) };
3
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);
4
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);
5
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;
6
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; }
7
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); }
8
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); } };
9
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 };
10
Class Student and StudentList
{ private: . . . public: void Read() void Write() const }; class StudentList
11
Class Student and StudentList
{ . . . }; class StudentList int main() StudentList cs143_S2; cs143_S2.Read(); cs143_S2.Write(); return 0; }
12
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; } };
13
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; }
14
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; };
15
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; }
16
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]; } };
17
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; }
18
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[] };
19
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; } };
20
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;
21
Schedule Quiz7-4 Lab 9 Program 5 Pairs Sign up Today
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.