#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string num;
string name;
char sex;
float english,math,history,physics;
public:
friend void calculate(Student &stu);
Student(string nu,string na,char se='M',float en=0,float ma=0,float hi=0,float ph=0)
{
num=nu;
name=na;
sex=se;
english=en;
math=ma;
history=hi;
physics=ph;
}
string getName()
{
return name;
}
// 重载 < 运算符
bool operator <( Student& stu)
{
float sum1=0,sum2=0;
sum1=english+math+physics+history;
sum2=stu.english+stu.math+stu.physics+stu.history;
if(sum1 < sum2)
{
return true;
}
return false;
}
// 重载 > 运算符
bool operator >( Student& stu)
{
float sum1=0,sum2=0;
sum1=english+math+physics+history;
sum2=stu.english+stu.math+stu.physics+stu.history;
if(sum1 > sum2)
{
return true;
}
return false;
}
// 重载 == 运算符
bool operator ==( Student& stu)
{
float sum1=0,sum2=0;
sum1=english+math+physics+history;
sum2=stu.english+stu.math+stu.physics+stu.history;
if(sum1 == sum2)
{
return true;
}
return false;
}
};
void calculate(Student& stu)
{
float sum,ave;
sum=stu.english+stu.math+stu.physics+stu.history;
ave=sum/4;
cout<<stu.name<<"的总成绩:"<<sum<<",平均成绩:"<<ave<<endl;
}
// 程序的主函数
int main( )
{
Student stu1("001","Jone",'M',80,80,80,80);
Student stu2("002","Jane",'F',80,80,80,80);
Student stu3("003","Ann",'F',81,82,83,84);
calculate(stu1);
calculate(stu2);
calculate(stu3);
if (stu1==stu2)
cout<<stu1.getName()<<"与"<<stu2.getName()<<"总成绩一样。"<<endl;
if (stu3>stu2)
cout<<stu3.getName()<<"的总成绩高于"<<stu2.getName()<<"的总成绩。"<<endl;
if (stu1<stu3)
cout<<stu1.getName()<<"的总成绩低于"<<stu3.getName()<<"的总成绩。"<<endl;
return 0;
}
Jone的总成绩:320,平均成绩:80
Jane的总成绩:320,平均成绩:80
Ann的总成绩:330,平均成绩:82.5
Jone与Jane总成绩一样。
Ann的总成绩高于Jane的总成绩。
Jone的总成绩低于Ann的总成绩。
|