(a)编写一个抽象类student,仅在public中包含一个纯虚函数printScore; (b)编写一个高中生类senior,公共继承自student;数据成员包括物理和化学课成绩:physics和chemistry,都是double类型;编写构造函数;编写函数printScore打印这两门课的成绩; (c)编写一个大学生college类,公共继承自senior,数据成员包括面向对象课成绩:OOP,double类型;编写构造函数;编写函数printScore打印物理、化学和面向对象的成绩。 (d)主函数中定义一个senior对象,物理和化学课成绩分别为85和90;定义一个college对象,物理、化学,以及面向对象课成绩分别为85、90和95,利用多态调用printScore函数打印两个对象的成绩。
时间: 2024-02-16 15:00:32 浏览: 126
下面是实现代码:
```cpp
#include <iostream>
using namespace std;
// 抽象类student
class student {
public:
virtual void printScore() = 0; // 纯虚函数
};
// 高中生类senior
class senior : public student {
public:
double physics;
double chemistry;
senior(double p, double c) : physics(p), chemistry(c) {}
void printScore() {
cout << "Physics: " << physics << ", Chemistry: " << chemistry << endl;
}
};
// 大学生类college
class college : public senior {
public:
double OOP;
college(double p, double c, double o) : senior(p, c), OOP(o) {}
void printScore() {
cout << "Physics: " << physics << ", Chemistry: " << chemistry << ", OOP: " << OOP << endl;
}
};
int main() {
senior s(85, 90);
college c(85, 90, 95);
student* p = &s;
p->printScore(); // 输出:Physics: 85, Chemistry: 90
p = &c;
p->printScore(); // 输出:Physics: 85, Chemistry: 90, OOP: 95
return 0;
}
```
输出结果:
```
Physics: 85, Chemistry: 90
Physics: 85, Chemistry: 90, OOP: 95
```