//********************************************* // COURSE.H - Course and Registration classes //********************************************* // Chapter 2 sample application. #include #include const unsigned CnameSize = 10; class Course { public: Course(); void Input( ifstream & infile ); void Output( ofstream & ofile ) const; private: char name[CnameSize]; // course name char section; // section (letter) unsigned credits; // number of credits }; const unsigned MaxCourses = 10; class Registration { public: Registration(); void Input( ifstream & infile ); void Output( ofstream & ofile ) const; private: long studentId; // student ID number unsigned semester; // semester year, number unsigned count; // number of courses Course courses[MaxCourses]; // array of courses }; //************************************************* // REGIST.CPP - Course and Registration class // implementations, and main program. //************************************************* #include "course.h" Course::Course() { name[0] = '\0'; } void Course::Input( ifstream & infile ) { infile >> name >> section >> credits; } void Course::Output( ofstream & ofile ) const { ofile << " Course: " << name << '\n' << " Section: " << section << '\n' << " Credits: " << credits << endl; } Registration::Registration() { count = 0; } void Registration::Input( ifstream & infile ) { infile >> studentId >> semester >> count; for(int i = 0; i < count; i++) courses[i].Input( infile ); } void Registration::Output( ofstream & ofile ) const { ofile << "Student ID: " << studentId << '\n' << "Semester: " << semester << '\n'; for(int i = 0; i < count; i++) { courses[i].Output( ofile ); ofile << '\n'; } } // MAIN.CPP - Chapter 2 example // Main program for the Registration application #include "course.h" int main() { // Read a Registration object from an input // file and write it to an output file. ifstream infile( "rinput.txt" ); if( !infile ) return -1; Registration R; R.Input( infile ); ofstream ofile( "routput.txt", ios::app ); if( !ofile ) return -1; R.Output( ofile ); return 0; } // RINPUT.TXT file------------------ 102234 962 4 CHM_1020 C 3 MUS_1100 H 1 BIO_1040 D 4 MTH_2400 A 3