(1) 定义一个Score类,其中包括私有数据成员和公有成员函数,即
发布网友
发布时间:2022-04-24 13:12
我来回答
共1个回答
热心网友
时间:2023-10-14 00:14
//Score.h
#ifndef SCORE_H
#define SCORE_H
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
class Score
{
public:
Score();
~Score();
void Inscore(int num, double m, double e, double p);
void Showscore();
private:
int mNum;
double mMath;
double mEnglish;
double mProgramming;
double mAver;
};
#endif
//Score.cpp
#include "Score.h"
Score::Score()
{
}
Score::~Score()
{
}
void Score::Inscore(int num, double m, double e, double p){
this->mNum=num;
this->mMath=m;
this->mEnglish=e;
this->mProgramming=p;
this->mAver=(m+e+p)/3;
}
void Score::Showscore(){
cout<<"Num: "<<(this->mNum)<<endl;
cout<<"Math: "<<(this->mMath)<<endl;
cout<<"English: "<<(this->mEnglish)<<endl;
cout<<"Programming: "<<(this->mProgramming)<<endl;
cout<<"Average: "<<(this->mAver)<<endl;
}
//main.cpp
#include "Score.h"
int main()
{
int n;
cout<<"Enter the numbers of students"<<endl;
cin>>n;
Score* students=new Score [n];
for(int i=0; i!=n; i++){
int numt;
double mt, et, pt;
cout<<"Enter num: "<<endl;
cin>>numt;
cout<<"Enter math: "<<endl;
cin>>mt;
cout<<"Enter english: "<<endl;
cin>>et;
cout<<"Enter programming: "<<endl;
cin>>pt;
students[i].Inscore(numt,mt,et,pt);
}
for(int i=0; i!=n; i++){
students[i].Showscore();
}
delete[] students;
return 0;
}