题目描述
定义Person类具有姓名、年龄等属性,具有输出基本信息的display函数。
选修《面向对象程序设计》课程的学生在Person类的基础上,派生出子类:免听生和非免听生。子类继承父类成员,新增其他成员、改写display函数。
非免听生具有平时成绩、考试成绩和总评成绩三个属性,总评成绩根据(平时成绩*40%+考试成绩*60%)计算的结果,85分(包含)以上为A,75分(包含)-85分(不包含)为B,65分(包含)-75分(不包含)为C,60分(包含)-65分(不包含)为D,60分(不包含)以下为F。
免听生只有考试成绩和总评成绩两个属性,总评成绩100%根据考试成绩对应上述等级制成绩。
定义上述类并编写主函数,输入类型符号,若输入R,根据学生基本信息、平时成绩和考试成绩,建立非免听生对象,若输入S,根据学生基本信息、考试成绩,建立免听生对象。计算学生的总评成绩,并输出。
输入
测试次数t
随后每行输入学生类型 相关信息,姓名的最大字符长度为20
输出
每个学生基本信息和总评成绩
#include<iostream>
#include<cmath>
using namespace std;
class person1
{
protected:
string name;
int age,a;
char le;
public:
person1(string nn, int aaa, int aa) :name(nn),age(aa),a(aa)
{
}
void display()
{
int s = a;
if (s >= 85)
{
le = 'A';
}
else if (s >= 75)
le = 'B';
else if (s >= 65)
le = 'C';
else if (s >= 60)
{
le = 'D';
}
else le = 'F';
cout << name << " " << age << " " << le << "\n";
}
};
class person2 :public person1
{
private:
int b;
public:
person2(string nn,int aaa,int aa,int bb) :person1(nn,aaa,aa), b(bb)
{
double s = 1.0 * a * 2 / 5 + 1.0 * b * 3 / 5;
if (s >= 85)
{
le = 'A';
}
else if (s >= 75)
le = 'B';
else if (s >= 65)
le = 'C';
else if (s >= 60)
{
le = 'D';
}
else le = 'F';
cout << name << " " << age << " " << le << "\n";
}
};
int main()
{
int n;
int age,a,b;
string name;
char c;
cin >> n;
while (n--)
{
cin >> c;
if (c == 'R')
{
cin >> name >> age >> a >> b;
person2 aa(name, age, a,b);
}
else
{
cin >> name >> age >> a;
person1 aa(name, age, a);
aa.display();
}
}
}