// student7.cc #include #include using namespace std; class Student { private: string Name; bool Auditor; double *Grades; public: // Constructor Student(string theName, bool isAuditor = false) :Name(theName), Auditor(isAuditor) { Grades = new double[3]; for (int i=0; i<3; i++) Grades[i] = 0.0; } // Copy constructor // not needed here b/c we pass refs now not objects to PrintStudentGrades Student(const Student& Source) { cout << "In copy constructor!" << endl; Name = Source.Name; Auditor = Source.Auditor; Grades = new double[3]; for (int i=0; i<3; i++) Grades[i] = Source.Grades[i]; } // Destructor ~Student() { delete [] Grades; // delete the dynamically allocated array } void SetGrade(int whichGrade, double grade) { Grades[whichGrade] = grade; } // friend declaration gives function outside class access to private bits friend void PrintStudentGrades(const Student& Input); }; // done defining and declaring Student class void PrintStudentGrades(const Student& Input) { if (Input.Auditor) cout << "Auditor "; else cout << "Student "; cout << Input.Name << " grades: "; cout << Input.Grades[0] << "," << Input.Grades[1] << "," << Input.Grades[2]; cout << ". Course grade: "; cout << (Input.Grades[0] + Input.Grades[1] + Input.Grades[2]) / 3.0 << endl; } int main() { Student Student1("John Smith"); // no 2nd argument given, assumes default Student1.SetGrade(0, 80.0); Student Student2("Jane Doe", true); Student2.SetGrade(0, 100.0); PrintStudentGrades(Student1); PrintStudentGrades(Student2); }