class Score
{
private:
string name;
double score;
static double TotalScore;
static double AverageScore;
static int num; //计算平均分需要
public:
Score(string n="", double s=0):name(n), score(s){}
void Set(string n, double s){name=n; score=s;} //定义数组对象时,需要对每个对象重新赋值
static void Output(){cout << "TotalScore = " << TotalScore << ", AverageScore = " << AverageScore << endl;}
friend void ComputeTotalScore(const Score& a);
};
double Score::TotalScore = 0;
double Score::AverageScore = 0;
int Score::num = 0;
void ComputeTotalScore(const Score& a)
{
Score::TotalScore += a.score;
Score::num++;
Score::AverageScore = Score::TotalScore/Score::num;
}
void main()
{
Score arr[10];
string name = "";
double score = 0;
for(int i=0; i<10; i++)
{
cout << "Input " << i+1 << " Student Name and Score: ";
cin >> name >> score;
arr[i].Set(name, score);
ComputeTotalScore(arr[i]);
}
arr[9].Output();
}
温馨提示:答案为网友推荐,仅供参考