C++定义一个Student类
定义一个Student类,在该类定义中包括:一个数据成员score(分数)及两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount...
定义一个Student类,在该类定义中包括:一个数据成员score(分数)及两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(float s)用于设置分数、求总分和累计学生人数;静态成员函数sum用于返回总分;静态成员函数average用于求平均值。在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。
发送到邮箱1354149078@qq.com 展开
发送到邮箱1354149078@qq.com 展开
1个回答
展开全部
又要交作业了吧?
#include <iostream.h>
class Student
{
private:
double score;
static double total;
static int count;
public:
Student(double s)
{
score=s;
total+=s;
count++;
}
static double sum()
{
return total;
}
static double average()
{
return total/count;
}
};
double Student::total=0.0;
int Student::count=0;
void main()
{int i,n;
double sco;
cout<<"Input the number of student";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Input the score:";
cin>>sco;
Student obj(sco);
}
cout<<"The total score is:"<<Student::sum()<<endl;
cout<<"The average score is:"<<Student::average()<<endl;
}
#include <iostream.h>
class Student
{
private:
double score;
static double total;
static int count;
public:
Student(double s)
{
score=s;
total+=s;
count++;
}
static double sum()
{
return total;
}
static double average()
{
return total/count;
}
};
double Student::total=0.0;
int Student::count=0;
void main()
{int i,n;
double sco;
cout<<"Input the number of student";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Input the score:";
cin>>sco;
Student obj(sco);
}
cout<<"The total score is:"<<Student::sum()<<endl;
cout<<"The average score is:"<<Student::average()<<endl;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询