定义一个Score类,其中包括私有数据成员和公有成员函数,即
mNum 学号
mMath 高等数学成绩
mEnglish 英语成绩
mProgramming 程序设计成绩
Inscore() 输入学号和各科成绩,并且计算平均成绩
Showscore() 输出学号和各科成绩
使用Score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。
#include "pch.h"
#include "Score.h"
#include <iostream>
using namespace std;
int main()
{
int n,a,b,c,d;
cin >> n;
Score *m = new Score[n];
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c >> d;
m[i].Inscore(a,b,c,d);
}
cout << "num" << '\t' << "math" << '\t' << "English" << '\t' << "programming" << '\t' << "average"<<endl;
for (int i = 0; i < n; i++)
{
m[i].Showscore();
}
delete[]m;
}
#pragma once
class Score
{
public:
Score();
double Inscore(double num, double math, double eng, double pro);
void Showscore();
private:
double mNum, mMath, mEnglish, mProgramming;
};
//Score cpp
#include "pch.h"
#include "Score.h"
#include <iostream>
using namespace std;
Score::Score()
{
}
double Score::Inscore(double num, double math, double eng, double pro)
{
mNum = num;
mMath = math;
mEnglish = eng;
mProgramming=pro;
return ( math + eng + pro) / 3;
}
void Score::Showscore()
{
cout << mNum << '\t' << mMath << '\t' << mEnglish << '\t' << mProgramming << '\t' << Score::Inscore(mNum, mMath, mEnglish, mProgramming)<<endl;
}