定义一个类Student,记录学生C++课程的成绩。要求使用静态数据成员或静态成员函数计算全班学生C++课程的总成绩和平均成绩。
输入格式:
输入5个不超过100的正整数,作为C++成绩。
输出格式:
在第一行中输出成绩的和,第二行输出平均成绩。
输入样例:
90 80 70 60 50
输出样例:
350
70
代码实现:
#include <iostream>
using namespace std;
class Student
{
static int total_score;
static float average;
int score;
public:
void Setscore()
{
int x;
cin >> x;
score = x;
}
int add()
{
total_score += score;
return total_score;
}
int aver()
{
average *= total_score/5;
}
void show()
{
cout << total_score << endl;
cout << average << endl;
}
};
int Student::total_score = 0;
float Student::average = 1;
int main()
{
Student a[5];
int i;
for (i=0;i<5;i++)
{
a[i].Setscore();
a[i].add();
}
a[i].aver();
a[i].show();
return 0;
}