#include<iostream>
#include<cstring>
class student
{private:
std::string sname;
int grade;
public:
student():sname(0),grade(0){}
student(std::string name,int g=0)
{sname=name;
grade=g;
}
student(student &stu)
{sname=stu.getSname();
grade=stu.getGrade();
}//拷贝构造函数
std::string& getSname()
{return sname;}
int getGrade()
{return grade;}
student& operator=(student &stu)
{sname=stu.getSname();
grade=stu.getGrade();
return stu;
}
bool operator>(const student&);
bool operator<(const student&);
bool operator==(const student&);
bool operator!=(const student&);
void setGrade(int g)
{grade=g;}
void display();
void GradeTransition();//用于百分制到5分制的转换
~student(){}
};
bool student::operator>(const student &stu)
{if(this.grate>stu.getGrade())//当THIS对象的成绩大于STU的成绩时
{std::cout<<sname<<"成绩大于"<<stu.getSname();
return true;
}
else
{std::cout<<sname<<"成绩不大于"<<stu.getSname();
return false;}
}
bool student::operator<(const student &stu)
{if(this.grade<stu.getGrade())
{std::cout<<this.sname<<"成绩小于"<<stu.getSname();
return true;
}
else
{std::cout<<this.sname<<"成绩不小于"<<stu.getSname();
return false;
}
}
bool student::operator==(const student &stu)
{if(this.grade==getGrade())
{std::cout<<this.sname<<"成绩等于"<<stu.getSname();
return true;
}
else
{std::cout<<this.sname<<"成绩不等于"<<stu.getSname();
return false;
}
bool student::operator!=(const student &stu)
{if(this.grade!=stu.getGrade())
{std::cout<<this.sname<<"成绩不等于"<<stu.getSname();
return true;
}
else
return false;
}
void student::display()
{std::cout<<"姓名:"<<this.sname<<std::endl;
std::cout<<"成绩:"<<grade<<std::endl;
}
void student::GradeTransition()
{if(grate<=100&&grate>89)
{display();
std::cout<<"五分制成绩为:"<<'A'<<std::endl;
}
else if(grate>79)
{{display();
std::cout<<"五分制成绩为:"<<'B'<<std::endl;
}
else if(grate>69)
{{display();
std::cout<<"五分制成绩为:"<<'C'<<std::endl;
}
else if(grate>59)
{{display();
std::cout<<"五分制成绩为:"<<'D'<<std::endl;
}
else
{display();
std::cout<<"五分制成绩为:"<<'E'<<std::endl;
}
}
int main()
{
student stu1(wang,80)
stu1.display();
stu1.GradeTransition();
student stu2(stu1);
stu2.display();
stu2.GradeTransition();
stu1.setGrade(59);
stu1.display();
stu1.GradeTransition();
stu2.display();
stu2.GradeTransition();
}
搞了一个下午都没搞好 希望能得到高手的帮助 谢谢