class constructors class student { private: string id, firstname, lastname; float gpa; public:...
TRANSCRIPT
![Page 1: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/1.jpg)
Class Constructorsclass 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
![Page 2: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/2.jpg)
Creating Class Objects// Must have the default constructorStudent 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
![Page 3: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/3.jpg)
Creating Class Objects// Works even without default constructorStudent s = Student(“Qi”, “Yang”);
// Override data of ss.Read();
// Creating an object and assign it to ss = Student(“13579”, “Joe”, “Clifton”, 3.9);
string first, last;float myGPA;
cin >> last >> first >> myGPA;
// Creating an object and assign it to ss = Student(first, last, myGPA);
3
![Page 4: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/4.jpg)
Array of Objects
int numStudents;string first, last;float myGPA;
// Must have default constructorStudent s, allStudents[30];
cin >> last >> first >> myGPA;
// Creating an object and assign it to ss = Student(first, last, myGPA);
// Copy s to allStudents[0]allStudents[0] = s;
4
![Page 5: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/5.jpg)
Array of Objectsint 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
![Page 6: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/6.jpg)
Array of Objectsint 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
![Page 7: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/7.jpg)
Class StudentListclass 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
![Page 8: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/8.jpg)
Class StudentListclass 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
![Page 9: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/9.jpg)
Class Student and StudentListclass Student
{
private:
. . .
public:
void Read()
void Write() const
};
class StudentList{private: . . .public: void Read() void Write() const};
9
![Page 10: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/10.jpg)
Class Student and StudentListclass Student
{
. . .
};class StudentList{ . . .};
int main(){ StudentList CS143_S2;
CS143_S2.Read();
CS143_S2.Write(); . . . return 0;}
10
![Page 11: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/11.jpg)
More Methods for Class StudentListclass 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
![Page 12: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/12.jpg)
Class Student and StudentListclass 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
![Page 13: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/13.jpg)
More Methods for Class StudentListclass 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
![Page 14: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/14.jpg)
Class Student and StudentListclass 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
![Page 15: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/15.jpg)
More Methods for Class StudentListclass 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
![Page 16: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/16.jpg)
Class Student and StudentListclass 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
![Page 17: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/17.jpg)
More Methods for Class StudentListclass 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
![Page 18: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/18.jpg)
More Methods for Class StudentListclass 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
![Page 19: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/19.jpg)
Class Student and StudentListint 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
![Page 20: Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string](https://reader036.vdocument.in/reader036/viewer/2022082507/5697bfeb1a28abf838cb7e48/html5/thumbnails/20.jpg)
Schedule
• Lab 9: Grace Next Wednesday
• Program 5
Pairs Today
20