关于C++的问题
哪位高手能告诉我在下面那道程序中:如果将staticfloataverage;声明中的static给删除掉为什么会出现编译错误?同样是成员函数voidtotal()为什么...
哪位高手能告诉我在下面那道程序中:如果将static float average; 声明中的static给删除掉为什么会出现编译错误?同样是成员函数void total()为什么可以访问静态成员数据sum 和count,而为什么上述中将static删除后却出现了编译错误,这是为什么?
请帮我详细点解释下
程序如下:
#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 stud[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++)
stud[i].total();
cout<<"the average score of"<<n<<"student is"<<student::average()<<endl;
return 0;} 展开
请帮我详细点解释下
程序如下:
#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 stud[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++)
stud[i].total();
cout<<"the average score of"<<n<<"student is"<<student::average()<<endl;
return 0;} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询