题目
定义一个score类,其中包括私有数据成员和公有成员函数,即
num 学号
Math 高等数学成绩
English 英语成绩
Programming 程序设计成绩
inscore() 输入学号和各科成绩,并且计算平均成绩
showscore(时) 输出学号和各科成绩
使用score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。
在这里插入代码片
#include <iostream>
using namespace std;
class score
{
private:
int num;
int math, english, programming;
double a;
public:
void inscore(int n, int m, int e, int p);
void showscore();
};
void score::inscore(int n,int m,int e,int p)
{
num = n;
math = m;
english = e;
programming = p;
//平均
a = (m+e+p) / 3;
}
void score::showscore()
{
cout << "学号:" << "\t" << "数学" << "\t" << "英语" << "\t" << "程序设计" << "\t" << "平均分" << endl;
cout << num << "\t" << math << "\t" << english << "\t" << programming << "\t"<<"\t" << a;
}
int main()
{
//输入学生数
int x;
cout << "请输入学生的数目" << endl;
cin >> x;
cout << endl;
//申请对象数组
score* stu;
stu = new score[x];
//输入各个学生成绩
int nn=0;
double mm=0, ee=0, pp=0;
for (int j = 0; j < x; j++)
{
cout << "学号 ";
cin >> nn;
cout << "数学成绩 ";
cin >> mm;
cout << "英语成绩 ";
cin >> ee;
cout << "程序设计成绩 ";
cin >> pp;
stu[j].inscore(nn,mm,ee,pp);
cout << endl;
}
//列表输出学生成绩
for (int q = 0; q < x; q++)
{
stu[q].showscore();
cout << endl;
}
return 0;
}