编程实现一个student类,管理一个学生的基本信息,包括学生姓名、学号、语文成绩、数学成绩、英语成绩等数据成员,然后在main函数中,从键盘输入得到一个学生的基本信息,最后输出所有数据成员值到屏幕上;
在以上student类的基础上分别用成员函数和友元函数两种形式重载“= =”运算符,判
断二个student对象的成绩是否相等(判定依据是三门平均分);
在以上student类的基础上分别用成员函数和友元函数两种形式重载“>”运算符,判断
二个student对象的成绩关系。
并在main函数中完成两个对象的“= =”和“>”运算功能。
华东理工大学是吧
试试。
#include <iostream>
using namespace std;
#define EPS 1e-3
class Student
{
std::string name_;
std::string no_;
int chineseScore_;
int mathsScore_;
int englishScore_;
public:
Student() {
name_ = "";
no_ = "";
chineseScore_ = 0;
mathsScore_ = 0;
englishScore_ = 0;
}
~Student() {
}
void print() {
cout << "name: " << name_.c_str() << endl;
cout << "no: " << no_.c_str() << endl;
cout << "chineseScore: " << chineseScore_ << endl;
cout << "mathsScore: " << mathsScore_ << endl;
cout << "englishScore: " << englishScore_ << endl;
}
void setName(const std::string& name) {
name_ = name;
}
void setNo(const std::string& no) {
no_ = no;
}
void setChineseScore(int score) {
chineseScore_ = score;
}
void setMathsScore(int score) {
mathsScore_ = score;
}
void setEnglishScore(int score) {
englishScore_ = score;
}
float getAverage() const {
return (chineseScore_ + mathsScore_ + englishScore_) / 3;
}
bool operator == (const Student &c1) const;
bool operator > (const Student &c1) const;
/*friend bool operator == (const Student &c1, const Student &c2) ;
friend bool operator > (const Student &c1, const Student &c2) ;*/
};
bool Student::operator == (const Student &s1) const
{
return fabs(getAverage() - s1.getAverage()) < EPS;
}
bool Student::operator > (const Student &s1) const
{
return getAverage() > s1.getAverage();
}
//bool operator == (const Student &s1, const Student &s2)
//{
// return s1.getAverage() == s2.getAverage();
//}
//
//bool operator > (const Student &s1, const Student &s2)
//{
// return s1.getAverage() > s2.getAverage();
//}
int main()
{
char name[255];
char no[255];
int chineseScore = 0;
int mathsScore = 0;
int englishScore = 0;
cout << "input:name no chineseScore mathsScore englishScore" << endl;
// input first student
cin >> name >> no >> chineseScore >> mathsScore >> englishScore;
Student s1;
s1.setName(name);
s1.setNo(no);
s1.setChineseScore(chineseScore);
s1.setMathsScore(mathsScore);
s1.setEnglishScore(englishScore);
s1.print();
// input second student
cin >> name >> no >> chineseScore >> mathsScore >> englishScore;
Student s2;
s2.setName(name);
s2.setNo(no);
s2.setChineseScore(chineseScore);
s2.setMathsScore(mathsScore);
s2.setEnglishScore(englishScore);
s2.print();
// compare two students
if (s1 == s2) {
cout << "s1 == s2" << endl;
}
else if (s1 > s2) {
cout << "s1 > s2" << endl;
}
else {
cout << "s1 < s2" << endl;
}
}