您的位置:首页 > 其它

使用静态成员函数统计平均成绩

2013-07-28 21:17 309 查看

#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();
private:
int num;
int age;
float score;
static float sum;
static int count;
};
void Student::total()      //改变静态成员的值,则在各个对象中这个数据成员的值都同时改变。可节约空间 ,提高效率。
{
sum=+score;
count++;
}
float Student::average()
{
return(sum/count);
}
float Student::sum=0;
int Student::count=0;       //对静态数据成员初始化。
int main()
{
Student stu[3]={Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)};
int n;
cout<<"please input the number of student:  ";
cin>>n;
for(int i=0;i<n;i++)
stu[i].total();
cout<<"the average of the score is :"<<Student::average()<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: