当前位置:网站首页>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;
}
}边栏推荐
- 515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
- Separation of storage and computing in Dahua cloud native database
- Use text analysis to identify the main gender in a text
- 深度学习——几种学习类型
- GBASE 8s存储过程语法结构
- js中的concat()
- Record of the 25th week
- PostgreSQL database Wal - RM_ HEAP_ ID logging action
- GBASE 8s 索引R树
- php开发支付宝支付功能之扫码支付流程图
猜你喜欢

【FLink】access closed classloader classloader.check-leaked-classloader

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

高效的NoSQL数据库服务Amozon DynamoDB体验分享

halcon之区域:多种区域(Region)生成(3)

Records of ros2/dds/qos/ topics

unity Quad剔除背面并剔除透明部分的shader

Record the problem of C # print size once

Value transfer between parent and child components of wechat applet
![[untitled]](/img/68/5e711f7c473dcea54a56f7b7e48604.png)
[untitled]

在 .NET 6 中使用 dotnet format 格式化代码
随机推荐
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
写shell脚本报错总结
[Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
The solution of wechat applet switchtab unable to take parameters
Machine learning deep learning -- Vectorization
GBase 8s的封锁技术的基本介绍
515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
【esp32学习之路6——flash加密】
Vscode 设置clang-format
GBASE 8s存储过程语法结构
Classification of gbase 8s locks
Wechat likes to pay attention to the solution of invalid automatic reply
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
执行SQL响应比较慢,你有哪些排查思路?
SQL injection details
Records of ros2/dds/qos/ topics
Excel exports data to SQL and pictures to folder through macro | VBA
Data import and export for gbase 8s
我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
php封装curl发送get、post请求方法,并使用