定义一个Student类,在该类定义中包括:一个数据成员(分数score)及两个静态数据成员(总分total和学生人数count);成员函数scoretotalcount(double s)用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。
在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。
#include <iostream> using namespace std; class student { public: void scoretotalcount( double s ) { score = s; total = total + score; count++; } static double sum() { return total; } static double average() { return total / count; } private: double score; static double total; static double count; }; double student::total=0; double student::count=0; int main() { int i,n; double s; cout << "请输入学生人数:"; cin >> n; student stu; for( i=1; i<=n; i++ ) { cout << "请输入第" << i << "个学生的分数:"; cin >> s; stu.scoretotalcount( s );} cout << "总分:" << student::sum() << endl; cout << "平均分:" << student::average() << endl; }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题