24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 347  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 260求调剂 +4 朱芷琳 2026-04-02 4/200 2026-04-02 19:17 by zixin2025
[考研] 319求调剂 +15 太容易1018 2026-04-01 15/750 2026-04-02 19:02 by 科研小专家
[考研] 环境工程297分求调剂一志愿杭高院 +15 GENJIOW 2026-03-31 16/800 2026-04-02 17:56 by cyh—315
[考研] 085601一志愿北理325分求调剂 +3 找调剂,, 2026-04-02 3/150 2026-04-02 16:42 by asdfzly
[考研] 材料调剂 +6 懒羊羊轻置玉臀 2026-04-02 6/300 2026-04-02 14:47 by 5896
[考研] 265求调剂 +6 梁梁校校 2026-04-01 6/300 2026-04-02 14:29 by 客尔美德
[考研] 07生物学求调剂 一志愿同济大学359分 +3 LAMC. 2026-03-30 3/150 2026-04-02 10:26 by 18828373951
[考研] 348环境工程调剂 +3 吴彦祖24k 2026-04-01 3/150 2026-04-02 09:14 by nanaliuyun
[考研] 379求调剂 +3 ?苦瓜不苦 2026-04-01 3/150 2026-04-01 20:09 by 暮云清寒
[考研] 332求调剂 +8 Lyy930824@ 2026-03-29 8/400 2026-04-01 18:40 by 千钧澄玉宇啊
[考研] 309求调剂 +19 谁不是少年 2026-03-29 19/950 2026-04-01 15:47 by jp9609
[考研] 311求调剂 +10 李芷新1 2026-03-31 10/500 2026-04-01 14:38 by chenqifeng666
[考研] 环境工程 085701,267求调剂 +15 minht 2026-03-29 16/800 2026-04-01 10:13 by li_sujuan99
[考研] 材料工程085601数二英一335求调剂 +5 双马尾痞老板2 2026-03-31 5/250 2026-03-31 19:07 by Wang200018
[考研] 346求调剂 一志愿070303有机化学 +11 萝卜炖青菜 2026-03-28 12/600 2026-03-31 17:54 by xhai2011
[基金申请] 面上5B能上会吗? +8 redcom 2026-03-29 8/400 2026-03-31 15:53 by niuailing
[考研] 一志愿哈尔滨工业大学材料与化工方向336分 +13 辰沐5211314 2026-03-26 13/650 2026-03-31 14:37 by 记事本2026
[考研] 313求调剂 +6 卖个关子吧 2026-03-31 6/300 2026-03-31 10:58 by Jaylen.
[考研] 323分 食品与营养调剂 +3 嘿ooo 2026-03-31 3/150 2026-03-31 09:38 by longlotian
[考研] 求调剂 +10 家佳佳佳佳佳 2026-03-29 10/500 2026-03-30 18:34 by 544594351
信息提示
请填处理意见