定义一个类Student,记录学生C++课程的成绩。要求使用静态数据成员或静态成员函数计算全班学生C++课程的总成绩和平均成绩。
输入格式: 输入5个不超过100的正整数,作为C++成绩。
输出格式: 在第一行中输出成绩的和,第二行输出平均成绩。
输入样例: 90 80 70 60 50
输出样例: 350 70
我的代码:
#include <iostream>
using namespace std;
class Student{
static int totalscore;
int score;
public:
Student(int s){
score=s;
}
static void get(int score){
totalscore+=score;
}
static void output(){
cout<<totalscore<<endl;
totalscore/=5;
cout<<totalscore<<endl;
}
};
int Student::totalscore=0;
int main(){
int i,s;
for(i=0;i<5;i++){
cin>>s;
Student st(s);
st.get(s);
}
Student st(5);
st.output();
return 0;
}
版权声明:本文为qq_35793751原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。