24小时热门版块排行榜    

查看: 332  |  回复: 1

冰雨hust

铁虫 (小有名气)

[求助] 函数静态变量的程序求问题

#include
using namespace std;
class Student
{ public:
Student(int n,int a,float s):num(n),age(a),scored(s){}
void total();
static  float average();
private:
        int num; int age;float scored;
        static float sum;
        static int count;
};
void Student::total()
{sum+=scored;
count++;}
float Student::average()
{return(sum/count);}
float Student::sum=0;
int Student::count=0;
int main()
{Student std[3]={Student(100,20,50), Student(101,21,90),Student(102,21,10)};
int n;
cout<<"please enter the number of student:";
cin>>n;
for(int i=0;i std.total();
cout<<"he average score of the"<
return 0;
}

上面这段程序如果不将average函数定义为静态成员函数行不行?如果行的话,应该做怎样的修改,请教各位大神~~~~
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

感谢参与,应助指数 +1
上面这段程序如果不将average函数定义为静态成员函数行不行?如果行的话,应该做怎样的修改,请教各位大神~~~~
----------------
可以,把下面这2个static也删除了
static float sum;
static int count;
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2013-02-28 23:15:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 冰雨hust 的主题更新
信息提示
请填处理意见