Upload
bertram-clarke
View
221
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
Class Student and StudentListclass Student
{
private:
. . .
public:
void Read()
void Write() const
};
class StudentList{private: . . .public: void Read() void Write() const};
9
Class Student and StudentListclass Student
{
. . .
};class StudentList{ . . .};
int main(){ StudentList CS143_S2;
CS143_S2.Read();
CS143_S2.Write(); . . . return 0;}
10
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
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
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
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
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
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
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
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
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
Schedule
• Lab 9: Grace Next Wednesday
• Program 5
Pairs Today
20