//------------------------------------------------------------------- // File :course.C // Author :Alicia Thorsen // Course :CS1129 // // Implementation of Course class. //------------------------------------------------------------------- #include "course.h" #include "student.h" #include #include #include #include using namespace std; //------------------------------------------------------------------- // Precondition: none. // Postcondition: The roster array is created. //------------------------------------------------------------------- Course::Course() : numOfStudents(0), rosterCapacity(1) { roster = new Student[rosterCapacity]; } //------------------------------------------------------------------- // Precondition: none. // Postcondition: The roster array is deleted. //------------------------------------------------------------------- Course::~Course() { delete [] roster; roster = NULL; } //------------------------------------------------------------------- // Precondition: name and numericGrade contain the name and grade of a student. // Postcondition: Adds the student to the roster. //------------------------------------------------------------------- void Course::addStudent(string name, double numericGrade) { if (numOfStudents == rosterCapacity) resize(); roster[numOfStudents].setName(name); roster[numOfStudents].setNumericGrade(numericGrade); numOfStudents++; } //------------------------------------------------------------------- // Precondition: The roster array contains all the student information. // Postcondition: Letter grades are computed for all students. //------------------------------------------------------------------- void Course::calcLetterGrades() { for (int i = 0; i < numOfStudents; ++i) roster[i].calcLetterGrade(); } //------------------------------------------------------------------- // Precondition: The roster array contains all the student information. // Postcondition: All student data for this course is written to an output file. //------------------------------------------------------------------- void Course::outputCourseData(ofstream& fileOut) const { int tableWidth; // Left align columns. fileOut.setf(ios::left); // Write course name. fileOut << courseName << "\n" << endl; // Write table headings. fileOut << setw(NAME_COLUMN_WIDTH) << "Name" << setw(NUMERIC_GRADE_COLUMN_WIDTH) << "Numeric Grade" << "Letter Grade\n"; // Draw a line under table headings. tableWidth = NAME_COLUMN_WIDTH + (2 * NUMERIC_GRADE_COLUMN_WIDTH); for (int j = 0; j < tableWidth; ++j) fileOut << "="; fileOut << endl; // Write each student row. for (int i = 0; i < numOfStudents; ++i) { fileOut << setw(NAME_COLUMN_WIDTH) << roster[i].getName() << setw(NUMERIC_GRADE_COLUMN_WIDTH) << roster[i].getNumericGrade() << roster[i].getLetterGrade() << endl; } fileOut << endl; } //------------------------------------------------------------------- // Precondition: none. // Postcondition: The capacity of the roster array is increased. //------------------------------------------------------------------- void Course::resize() { int increment = 1; Student* temp = new Student[rosterCapacity + increment]; if (temp == NULL) { cerr << "Insufficient memory. Aborting." << endl; exit(1); } for (int i = 0; i < rosterCapacity; ++i) temp[i] = roster[i]; delete [] roster; roster = temp; rosterCapacity += increment; } //------------------------------------------------------------------- // Precondition: A course object to be copied is passed. // Postcondition: The calling object contains the same contents as the // passed object. Returns a reference to the calling object. //------------------------------------------------------------------- Course& Course::operator=(const Course& rhs) { for (int i = 0; i < rhs.numOfStudents; ++i) { if (numOfStudents == rosterCapacity) resize(); roster[i] = rhs.roster[i]; numOfStudents++; } courseName = rhs.courseName; return *this; }