当前位置:网站首页>Calculate student grade (virtual function and polymorphism)
Calculate student grade (virtual function and polymorphism)
2022-06-25 04:43:00 【SZU healing system bug】
Catalog
Title Description
Please design 3 Classes , They are students Student, Undergraduate class Undergraduate, Graduate students Postgraduate, among Student Class is the base class , It contains basic student information , Such as name 、 Category ( Undergraduate or graduate )、 Courses learned ( It is assumed that 3 Course , Express in an array ) Grades and grades ;Undergraduate Classes and Postgraduate All are Student A derived class of the , The main difference between them is the calculation 3 There are different ways of grading the average grade of each course , The standard of postgraduates is higher than that of undergraduates , As shown in the following table :
| Undergraduate standards | Graduate standards |
|---|---|
| 80~100 good | 90~100 good |
| 70~80 good | 80~90 good |
| 60~70 commonly | 70~80 commonly |
| 50~60 pass | 60~70 pass |
| 50 Fail below | 60 Fail below |
Student Student The base class framework is shown below :
class Student{
protected:
string name; // The student's name
int type; // Student categories :1 Means undergraduate ,2 Indicates a graduate student
int courses[3]; //3 Results of courses
string courseGrade; // Grades
public:
Student(string n,string t,int a1,int a2,int a3);// Construction method
virtual void calculateGrade()=0;// Calculate grade
void print();// Output information
};
With Student Base class , build Undergraduate、Postgraduate Two classes .
Generate the above class and write the main function , Require a base class pointer in the main function , Generate base class dynamic array , To receive subclass objects .
Input
The first line indicates the number of tests . Start with the second line , One line per test case , The meaning of each line of data is as follows : The student's name 、 Student categories (1 For undergraduates ,2 For graduate students )、3 Results of courses .
Output
The student's name 、 Category 、 Grades
sample input 1
sample output 1
AC Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
class Student {
protected:
string name; // The student's name
int type; // Student categories :1 Means undergraduate ,2 Indicates a graduate student
int courses[3]; //3 Results of courses
string courseGrade; // Grades
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;
}// Construction method
virtual void calculateGrade() = 0; // Calculate grade
void print() {
if (type == 1)
cout << name << ", Undergraduate ," << courseGrade << endl;
else
cout << name << ", Graduate student ," << 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 = " good ";
else if (average >= 70)
courseGrade = " good ";
else if (average >= 60)
courseGrade = " commonly ";
else if (average >= 50)
courseGrade = " pass ";
else
courseGrade = " fail, ";
}
};
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 = " good ";
else if (average >= 80)
courseGrade = " good ";
else if (average >= 70)
courseGrade = " commonly ";
else if (average >= 60)
courseGrade = " pass ";
else
courseGrade = " fail, ";
}
};
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;
}
}边栏推荐
- Excel exports data to SQL and pictures to folder through macro | VBA
- GBASE 8s存储过程语法结构
- cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
- Multithreading structure of gbase 8s
- Data import and export for gbase 8s
- GBASE 8s的包
- GBASE 8s存储过程流程控制
- i. Max development board learning record
- GBASE 8s活锁、死锁问题的解决
- 【Flink】RocksDB增量模式checkpoint大小持续增长的问题及解决
猜你喜欢

Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption

CTF_ Web:8-bit controllable character getshell

js中的concat()

leetcode1221. 分割平衡字符串

php开发支付宝支付功能之扫码支付流程图

mongodb集群

Value transfer between parent and child components of wechat applet

JS arguments

cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

Gbase 8s overall architecture
随机推荐
GBase 8s 锁的分类
华为鸿蒙开发第四课
Efficient NoSQL database service Amazon dynamodb experience sharing
GBASE 8s的数据视图
Value transfer between parent and child components of wechat applet
第九章 APP项目测试(2) 测试工具
Immutable學習之路----告別傳統拷貝
记录小知识点
大话云原生数据库中的存算分离
Classification of gbase 8s locks
STM32的DMA双缓冲模式详解
Web3 DApp用户体验最佳实践
Cannot import name 'escape' from 'jinja2' [solved successfully]
i. Max development board learning record
Record of the 25th week
CTF_ Web: Changan cup-2021 old but a little new & asuka
Trigger for gbase 8s
dotnet-exec 0.4.0 released
cannot import name ‘escape’ from ‘jinja2’【成功解决】
jsz中的join()