当前位置:网站首页>计算学生成绩等级(虚函数和多态)
计算学生成绩等级(虚函数和多态)
2022-06-25 04:01:00 【SZU治愈系BUG】
目录
题目描述
请设计3个类,分别是学生类Student,本科生类Undergraduate,研究生类Postgraduate,其中Student类是基类,它包含基本的学生信息,如姓名、类别(本科生还是研究生)、所学课程(这里假定为3门课,用数组表示)成绩和成绩等级等;Undergraduate类和Postgraduate都是Student类的派生类,它们之间的主要差别是计算3门课程平均成绩等级的方法有所不同,研究生的标准要比本科生的标准高一些,如下表所示:
| 本科生标准 | 研究生标准 |
|---|---|
| 80~100优秀 | 90~100优秀 |
| 70~80良好 | 80~90良好 |
| 60~70一般 | 70~80一般 |
| 50~60及格 | 60~70及格 |
| 50以下不及格 | 60以下不及格 |
学生Student基类框架如下所示:
class Student{
protected:
string name; //学生姓名
int type; //学生类别:1表示本科生,2表示研究生
int courses[3]; //3门课的成绩
string courseGrade; //成绩等级
public:
Student(string n,string t,int a1,int a2,int a3);//构造方法
virtual void calculateGrade()=0;//计算成绩等级
void print();//输出信息
};
以Student为基类,构建出Undergraduate、Postgraduate两个类。
生成上述类并编写主函数,要求主函数中有一个基类指针,生成基类动态数组,来接收子类对象。
输入
第一行表示测试次数。从第二行开始,每个测试用例占一行,每行数据意义如下:学生姓名、学生类别(1为本科生,2为研究生)、3门课的成绩。
输出
学生姓名、类别、成绩等级
输入样例1
输出样例1
AC代码
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
class Student {
protected:
string name; //学生姓名
int type; //学生类别:1表示本科生,2表示研究生
int courses[3]; //3门课的成绩
string courseGrade; //成绩等级
public:
Student(string n, int t, int a1, int a2, int a3): name(n), type(t) {
courses[0] = a1;
courses[1] = a2;
courses[2] = a3;
}//构造方法
virtual void calculateGrade() = 0; //计算成绩等级
void print() {
if (type == 1)
cout << name << ",本科生," << courseGrade << endl;
else
cout << name << ",研究生," << courseGrade << endl;
}
};
class Undergraduate: public Student {
public:
Undergraduate(string n, int t, int a1, int a2, int a3): Student(n, t, a1, a2, a3) {}
virtual void calculateGrade() {
int average = (courses[0] + courses[1] + courses[2]) / 3;
if (average >= 80)
courseGrade = "优秀";
else if (average >= 70)
courseGrade = "良好";
else if (average >= 60)
courseGrade = "一般";
else if (average >= 50)
courseGrade = "及格";
else
courseGrade = "不及格";
}
};
class Postgraduate: public Student {
public:
Postgraduate(string n, int t, int a1, int a2, int a3): Student(n, t, a1, a2, a3) {}
virtual void calculateGrade() {
int average = (courses[0] + courses[1] + courses[2]) / 3;
if (average >= 90)
courseGrade = "优秀";
else if (average >= 80)
courseGrade = "良好";
else if (average >= 70)
courseGrade = "一般";
else if (average >= 60)
courseGrade = "及格";
else
courseGrade = "不及格";
}
};
int main() {
int t, tpye, a, b, c, i;
string name;
cin >> t;
i = t;
Student** p = new Student*[t];
while (t--) {
cin >> name >> tpye >> a >> b >> c;
if (tpye == 1)
p[t] = new Undergraduate(name, tpye, a, b, c);
else
p[t] = new Postgraduate(name, tpye, a, b, c);
p[t]->calculateGrade();
p[t]->print();
}
if (p) {
for (t = 0; t < i; t++)
delete p[t];
delete[] p;
}
}边栏推荐
- Coinlist queuing tutorial to improve the winning rate
- Musk released humanoid robot. Why is AI significant to musk?
- mongodb集群
- [esp32 learning path 6 - Flash encryption]
- Smart contract learning materials
- PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
- Gbase 8s stored procedure flow control
- SQL注入详解
- CTF_ Web: Learn flask template injection (SSTI) from 0
- php封装curl发送get、post请求方法,并使用
猜你喜欢

jsz中的join()

CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)

什么是存储引擎以及MySQL常见的三种数据库存储引擎
![LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路

Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'

Simple text analysis of malicious samples - Introduction

Can Navicat directly operate the Android database SQLite

CTF_ Web:php weak type bypass and MD5 collision

CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)

Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
随机推荐
使用文本分析识别一段文本中的主要性别
Laravel document sorting 1. Installation and Preliminary Configuration
PHP extracts and analyzes table contents, and collects bidding information
Gbase 8s index b+ tree
LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
GBase 8s的封锁技术的基本介绍
Package for gbase 8s
Detailed explanation of flex attributes in flex layout
What is data persistence?
Can Navicat directly operate the Android database SQLite
Laravel document sorting 10. Request life cycle
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
Easyrecovery15 very easy to use computer data recovery software
GbASE 8s中的Blob 页(Blobspace page)
Failed to install redis interface
1. Phase II of the project - user registration and login
UCLA | 用于黑盒优化的生成式预训练
CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
小白学习MySQL - 统计的'投机取巧'