【问题描述】
某天,教务处要统计学生的C++成绩。
假设你是计算机某班的班长,现在已经知道了所有学生的学号,姓名,C++成绩,统计你班学生的人数与你班C++的平均成绩。请定义一个名为Student的类,其中,用成员数据myID来记录学生id,myName来记录学生姓名,myScore来记录学生成绩,并用静态成员数据classSize记录班级人数和静态成员数据totalScore记录总成绩,编写静态成员函数getNum()来统计班级人数,静态成员函数getAverage()来计算平均成绩。
#include <iostream>
#include <string>
using namespace std;
class Student{
private:
string myID;
string myName;
int myScore;
public:
Student(string id, string name, int score);
static int classSize;
static int totalScore;
static int getNum();
static double getAverage();
};
Student::Student(string id, string name, int score) {
myID = id;
myName = name;
myScore = score;
classSize++;
totalScore += score;
}
int Student::getNum() {
return classSize;
}
double Student::getAverage() {
return (double) totalScore / classSize;
}
int Student::classSize=0;
int Student::totalScore=0;
int main()
{
string id, name;
int score;
cout << "Please input the information of a student: (id, name and score). Press Ctrl+Z to stop." << endl;
while(cin>>id>>name>>score){
Student stu(id, name, score);
}
cout<<Student::getNum()<<" "<<Student::getAverage()<<endl;
return 0;
}