代码如下:
#include<iostream>
using namespace std;
#include<string.h>
class Grade{
public:
Grade(string name,int grade,char level);
Grade(Grade &G);
int const PrintData(string name,int grade);
int Getcount();
friend int Horl(Grade &m,Grade &n);//友元比较两个学生成绩的
void SetInfo(string name,int grade);
private:
string name;
int grade;
char level;
int static count;
int grading(int grade)//对应学生成绩等级
{
grade=grade/10;
if(grade>=0&&grade<=5) level='E';
if(grade==6) level='D';
if(grade==7) level='C';
if(grade==8) level='B';
if(grade==9||grade==10) level='A';
}
};
Grade::Grade(string name,int grade,char level):name(name),grade(grade),level(level)
{
cout<<"calling constructor of Grade"<<'\n';
}
Grade::Grade(Grade &G)
{
name=G.name;
grade=G.grade;
level=G.level;
cout<<"calling copy of constructor of Grade"<<'\n';
}
int Grade::count=0;
const int Grade::PrintData(string name,int grade)
{
grading(grade);
cout<<"学生姓名:"<<name<<'\n';
cout<<"成绩:"<<grade<<'\n';
cout<<"等级:"<<level<<'\n';
count=count+1;
}
Grade::Getcount()
{
cout<<"学生总数:"<<count<<'\n';
}
void Grade::SetInfo(string name,int grade)
{
this->name=name;
this->grade=grade;
}
int Horl(Grade &m,Grade &n)
{
int a=m.grade-n.grade;
if(a==0)
cout<<"两位同学成绩一样高"<<endl;
if(a>0){
cout<<m.name<<"同学成绩较高"<<'\n'<<"具体信息:"<<'\n';
m.PrintData(m.name,m.grade);
}
if(a<0){
cout<<n.name<<"同学成绩较高"<<'\n'<<"具体信息:"<<'\n';
n.PrintData(n.name,n.grade);
}
}
int main()
{
Grade *G[6];
string name;
int grade;
char level='0';
for(int i=0;i<2;i++)
{
cout<<"输入第"<<i+1<<"名学生名称:";
cin>>name;
cout<<"输入成绩:";
cin>>grade;
G[i]=new Grade(name,grade,level);
G[i]->SetInfo(name,grade);
}
cout<<"输出储存的信息:"<<'\n';
for(int i=0;i<2;i++)
{
G[i]->PrintData(name,grade);
delete G[i];
}
return 0;
}