设计一个学生类CStudent,其结构如下:
私有数据成员Name(学生姓名)、Degree(成绩);
构造函数对数据成员初始化;
设计一个友元函数Display(const CStudent &); 输出成绩的等级:>=90为优秀;80~89为良好;70~79为中等;60~69为及格;< 60为不及格;
输出结果如下:
姓名 成绩 等级
Mary 78 中等
Jack 93 优秀
Mike 54 不及格
John 88 良好
#include <iostream>
#include <string>
using namespace std;
class CStudent {
private:
string Name;
int Degree;
public:
CStudent(string name, int degree): Name(name), Degree(degree) {}
friend void Display(const CStudent &);
};
void Display(const CStudent &stu) {
cout << stu.Name << " " << stu.Degree << " ";
if (stu.Degree >= 90)
cout << "优秀" << endl;
else if (stu.Degree >= 80)
cout << "良好" << endl;
else if (stu.Degree >= 70)
cout << "中等" << endl;
else if (stu.Degree >= 60)
cout << "及格" << endl;
else
cout << "不及格" << endl;
}
int main() {
CStudent s1("Mary", 78);
CStudent s2("Jack", 93);
CStudent s3("Mike", 54);
CStudent s4("John", 88);
cout << "姓名 成绩 等级" << endl;
Display(s1);
Display(s2);
Display(s3);
Display(s4);
return 0;
}