1 //===================================== 2 // student.h 3 //===================================== 4 #ifndef HEADER_STUDENT 5 #define HEADER_STUDENT 6 //------------------------------------- 7 #include<iostream> 8 using namespace std; 9 //------------------------------------- 10 class Student{ 11 string name; 12 int semesterHours; 13 double average; 14 public: 15 Student(string pName="noName"):name(pName),average(0),semesterHours(0){} 16 void addCourse(int hours, double grade){ 17 double totalGrade = (semesterHours * average + grade); //总分 18 semesterHours += hours; //总修学时 19 average = semesterHours ? totalGrade/semesterHours : 0; //平均分 20 } 21 void display(){ 22 cout<<"name=/"<<name<<"/"<<",hours="<<semesterHours<<", average="<<average<<" "; 23 } 24 int getHours(){ return semesterHours; } 25 double getAverage(){ return average; } 26 };//----------------------------------- 27 #endif // HEADER_STUDENT 28 29 30