| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:本人自学C,现在自学C++,里面有个静态成员函数没弄明白
取消只看楼主 加入收藏
lyj23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:140
注 册:2010-10-31
结帖率:86.21%
收藏
已结贴  问题点数:10 回复次数:0 
本人自学C,现在自学C++,里面有个静态成员函数没弄明白
书上的例题:
程序代码:
#include<iostream>
using namespace std;
class Student
{
public:
  Sudent(int n,int a,float s):num(n),age(a),score(s){}
  void total();
  static float average();
private:
  int num;
  int age;
  float score;
  static float sum;
  static float count;
};
void Student::total()
{
  sum+=score;
  count++;
}
float Student::average()
{return(sum/count);}
float Student::sum=0;
float Student::count=0;
int main()
{Student stud[3]={
  Student(1001,18,70),
  Student(1002,19,78),
  Student(1005,20,98)
};
  int n;
  cout<<"please input 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"<<Student::average()<<endl;
  return 0;
}
这里有几个问题:
average函数如果定义为非静态成员函数行不行?
程序能否通过编译?
需要作什么修改?
为什么要用静态成员函数?

高手来啊!!
搜索更多相关主题的帖子: average 自学 
2011-03-27 17:18
快速回复:本人自学C,现在自学C++,里面有个静态成员函数没弄明白
数据加载中...
 
   



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

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