| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:为什么不能这样啊?
只看楼主 加入收藏
boyyang4894
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-2-13
收藏
 问题点数:0 回复次数:7 
为什么不能这样啊?

#include<iostream>
using namespace std;

class Student
{ public:
Student(int n,int a,float s):num(n),age(a),score(s){}
void total();
static float average();
friend void show(Student&);
private:
int num,age;
float score;
static float sum;
static int count;
};

void Student::total()
{ sum+=score;
count++;
}
float Student::average()
{ return (sum/count);}

void show(Student&t)
{
cout<<"the intformating of student is:"<<endl<<t.num<<endl<<t.age<<endl<<t.score<<endl;

}
int Student::count=0;
float Student::sum=0;
int main()
{ Student stud[3]={Student(120,22,100),Student(119,22,89),Student(118,23,60)};
int n;
cout<<"please enter the number of students:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
stud[i].total();
cout<<"the average score of"<<n<<"students is:"<<endl<<Student::average()<<endl;
for(int j=0;j<n;j++)
show(stud[j]);// 为什么不能这么做啊?我编译时进入了死循环.只有把两个for改为for(int i=0;i<n;i++){stud[i].total();show(stud[i]);}就可以了,为什么啊,谁能告诉我.谢谢
system("pause");
return 0;
}


搜索更多相关主题的帖子: void total private average include 
2007-02-23 15:59
boyyang4894
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-2-13
收藏
得分:0 
难道没有人知道么?
2007-02-23 16:53
dlcdavid
Rank: 3Rank: 3
来 自:成都
等 级:新手上路
威 望:6
帖 子:193
专家分:0
注 册:2005-12-23
收藏
得分:0 

可以运行得嘛

[此贴子已经被作者于2007-2-23 17:29:58编辑过]


为了C++,我放弃了课本
为了高考,我又放弃了C++
现在而今眼目下,我能做什么?www.
2007-02-23 17:07
dlcdavid
Rank: 3Rank: 3
来 自:成都
等 级:新手上路
威 望:6
帖 子:193
专家分:0
注 册:2005-12-23
收藏
得分:0 

............

[此贴子已经被作者于2007-2-23 17:30:20编辑过]


为了C++,我放弃了课本
为了高考,我又放弃了C++
现在而今眼目下,我能做什么?www.
2007-02-23 17:12
boyyang4894
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-2-13
收藏
得分:0 
难道我的VC++有错?
2007-02-23 17:33
deng1987
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-1-13
收藏
得分:0 
正常阿,你写的没问题阿,我试过了
2007-02-26 21:31
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

你的想法和你的做法有点出路:
你想用static变量来记录学生个数和学生成绩总分,但是却用total函数来做,这样虽然可以但是比较麻烦。
我给一种稍微简单点的做法:


#include<iostream>
using namespace std;

class Student
{
int num,age;
float score;
public:
Student(int n,int a,float s):num(n),age(a),score(s)
{
count++;
sum+=score;
}
friend void show(Student&);
static float average();
static float sum;
static int count;
};
float Student::average()
{
return (sum/count);
}
void show(Student&t)
{
cout<<\"the intformating of student is:\"<<endl<<t.num<<endl<<t.age<<endl<<t.score<<endl;
}

int Student::count=0;
float Student::sum=0;
int main()
{
Student stud[3]={Student(120,22,100),Student(119,22,89),Student(118,23,60)};
cout<<\"The average score of\"<<Student::count<<\"students is:\"<<endl<<Student::average()<<endl;
for(int j=0;j<Student::count;j++)
show(stud[j]);
system(\"pause\");
return 0;
}
注意:未编译,请自行编译调试

[此贴子已经被作者于2007-3-2 23:33:24编辑过]


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-03-02 23:30
boyyang4894
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-2-13
收藏
得分:0 
看到了,谢谢
2007-03-10 22:05
快速回复:为什么不能这样啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015304 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved