题目描述
定义Person类具有姓名、年龄等属性,具有输出基本信息的display函数。
选修《面向对象程序设计》课程的学生在Person类的基础上,派生出子类:免听生和非免听生。子类继承父类成员,新增其他成员、改写display函数。
非免听生具有平时成绩、考试成绩和总评成绩三个属性,总评成绩根据(平时成绩*40%+考试成绩*60%)计算的结果,85分(包含)以上为A,75分(包含)-85分(不包含)为B,65分(包含)-75分(不包含)为C,60分(包含)-65分(不包含)为D,60分(不包含)以下为F。
免听生只有考试成绩和总评成绩两个属性,总评成绩100%根据考试成绩对应上述等级制成绩。
定义上述类并编写主函数,输入类型符号,若输入R,根据学生基本信息、平时成绩和考试成绩,建立非免听生对象,若输入S,根据学生基本信息、考试成绩,建立免听生对象。计算学生的总评成绩,并输出。
输入
测试次数t
随后每行输入学生类型 相关信息,姓名的最大字符长度为20
输出
每个学生基本信息和总评成绩
输入样例1
2
R cindy 18 100 100
S sandy 28 59
输出样例1
cindy 18 A
sandy 28 F
该题主要考察对继承的应用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
class person
{
protected:
string name;
int age;
public:
person(string n, int ag)
{
name = n;
age = ag;
}
void display();
};
class rper :public person
{
protected:
int ga, gb;
char sum;
public:
void display();
rper(string name, int age, int a, int b);
};
class sper:public person
{
protected:
int gb;
char sum;
public:
sper(string name,int age,int b):person(name,age)
{
gb = b;
}
void display();
};
void rper::display()
{
cout << this->name << ' ' << this->age << ' ';
int s = ga * 0.4 + gb * 0.6;
if (s >= 0 && s < 60)cout << 'F' << endl;
else if (s < 65)cout << 'D' << endl;
else if (s < 75)cout << 'C' << endl;
else if (s < 85)cout << 'B' << endl;
else cout << 'A' << endl;
}
rper::rper(string name, int age, int a, int b):person(name,age)
{
ga = a;
gb = b;
}
void sper::display()
{
cout << this->name << ' ' << this->age << ' ';
if (gb >= 0 && gb < 60)cout << 'F' << endl;
else if (gb < 65)cout << 'D' << endl;
else if (gb < 75)cout << 'C' << endl;
else if (gb < 85)cout << 'B' << endl;
else cout << 'A' << endl;
}
int main()
{
char type;
string name;
int t,a,b,age;
cin >> t;
while (t--)
{
cin >> type;
if (type == 'R')
{
cin>> name >> age >> a >> b;
rper st(name, age, a, b);
st.display();
}
else if (type == 'S')
{
cin >> name >> age >> b;
sper st(name, age, b);
st.display();
}
}
return 0;
}