问题:
定义学生成绩类Score,其私有数据成员有学号、姓名、物理、数学、外语、平均成绩。补全Score类及主函数,使得程序能在一行中一次输出该生的学号、姓名、物理、数学、外语、平均成绩。
【输入形式】学生的学号、姓名、物理成绩、数学成绩、外语成绩。
【输出形式】除了输入的所有基本信息,还应包括计算的平均成绩。
【样例输入】
081531 WangXiaoming 100 82 99
【样例输出】
081531 WangXiaoming 100 82 99 93.6667
代码:
#include<iostream>
using namespace std;
//学生成绩类score
class score
{
//私有
private :
//学生学号,学生姓名
string stuId,stuname;
//学生的物理、数学、英语成绩
double physical,math,english;
//学生物理、数学、英语的平均成绩
double average;
//公共
public:
//构造函数
score(string StuId,string Stuname,double Physical,double Math,double English)
{
stuId=StuId;
stuname=Stuname;
physical=Physical;
math=Math;
english=English;
}
//求三科成绩的平均值
void Average()
{
average=(physical+math+english)/3;
}
//输出学生的学号、姓名、物理成绩、数学成绩、英语成绩以及三科成绩的平均成绩
void print()
{
cout<<stuId<<" "<<stuname<<" "<<physical<<" "<<math<<" "<<english<<" "<<average;
}
};
int main()
{
string StuId,Stuname;
double Physical,Math,English;
while(cin>>StuId>>Stuname>>Physical>>Math>>English)
{
//创建score的对象stu1,并完成初始化
score stu1(StuId,Stuname,Physical,Math,English);
//计算对象stu1的平均分数
stu1.Average();
//输出对象stu1的信息
stu1.print();
}
return 0;
}
运行结果: