// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 158. #include using namespace std; #include "student.h" float student_list::average_mark() const { float sum_marks = 0.0; for (int i = 0; i < num_students; i++) sum_marks += st[i]->get_mark(); return(sum_marks/num_students); } // The load_data method is the only one that needs to know the // format of the data in the database file. // This is shown in the following example line: // a Alice_Brown 12345678 75.2 82.3 // The first character indicates the stream. int student_list::load_data(const char *fname) { ifstream in(fname); if (!in) return(-1); in >> num_students; int i; char name[MAXNAMELENGTH]; long idnumber; float mark1, mark2; char type; for (i = 0; i < num_students; i++) { in >> type >> name >> idnumber >> mark1 >> mark2; // Now set up appropriate type of student structure // and set pointer in st[] if (type == 'b') st[i] = new b_stream_student(name, idnumber, mark1, mark2); else st[i] = new a_stream_student(name, idnumber, mark1, mark2); } in.close(); return(num_students); } int software_design::work_out_marks() { for (int i = 0; i < num_students; i++) st[i]->calc_mark(); return(0); }