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

大家看下这个程序,运行结果不对

醒山 发布于 2016-04-20 17:41, 4645 次点击
#include<iostream>
using namespace std;
class Student
{
public:
    float score;
    static float total;
    static float count;
     scoretotalcount(float = 0.0);
    static  float sum();
    static float average();
    fun(float = 0.0);
};
float Student::total = 0.0;
float   Student::count = 0.0;
Student::fun(float c)
{
    count = c;
}
Student::scoretotalcount(float s)
{
    score = s;
    total = total + score;
}
float Student::sum()
{
    return total;
}
float Student::average()
{
    return total / count;
}
int main()
{
    Student p;
    int i;
    p.fun(2.0);
    for (i = 0; i<2; i++)
    {
        float t;
        cin >> t;
        p.scoretotalcount(t);
    }
    cout << p.sum << endl;
    cout << p.average << endl;
    return 0;
}
4 回复
#2
yangfrancis2016-04-21 08:36
fun和scoretotalcount没有声明类型,调用sum和average时没有写参数表,即使不含参数也要写上括号。最可怖的是类里面score和sum, average这三个成员一起出现了。这个类到底是体现总分还是单科分数就说不清了。
#3
醒山2016-04-21 17:34
是的,说的好对,但是三个成员出现同时,应该不会说不清吧,那句的含义再说明下,谢谢

[此贴子已经被作者于2016-4-21 17:38编辑过]

#4
yangfrancis2016-04-21 19:34
score是某一科的分数,sum和average是所有分数合起来得到的(不知道是不是该这样理解),说明这个类本身就是处理某人所有学科的分数。因此我感觉score说不明白是具体哪一科的分数。要含单科就干脆把score弄成一个数组,几门课就是几个成员
#5
醒山2016-05-01 19:10
嗯嗯
1