注册 登录
编程论坛 C++教室

想请教一道编程题,请各位大虾进来指点迷津。

星罗棋布的 发布于 2012-04-19 12:44, 514 次点击
定义一个student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(float s)用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。在main()函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。求代码。
还有一个问题是:既然总分和总人数已经是数据成员了,为什么还要在其他的成员函数中求它们呢?谢谢各位了。
2 回复
#2
飞扬冲天2012-04-19 20:22
代码我觉得你应该先自己写一下,然后传上来大家研究,至于中分和总人数他们是静态成员啊,对象不能直接调用的..
#3
ab10349827492012-04-19 23:32
#include<iostream>
using namespace std;
const int SIZE =10;
class Student
{
private:
    static int count;
    static float total;
    float score;
public :
    void scoretotalcount(float s);
    static float sum();
    static float average();
};
int Student::count=0;
float Student::total=0;

void Student::scoretotalcount(float s)
{
    score =s;
    count++;
    total=total+score;
}
float Student::sum()
{
    return total;
}
float Student::average()
{
    return total/count;
}
Student student[SIZE];
int main(void)
{
    float grade;
    int i=0;
    cout<<"请输入每个人的成绩:"<<endl;
    for(i=0;i<SIZE;i++)
    {
        cin>>grade;
        student[i].scoretotalcount(grade);
    }
    cout<<"全班总分为:"<<Student::sum()<<endl;
    cout<<"全班成绩平均分为:"<<Student::average()<<endl;
    return 0;
}
你参考一下吧。
1