题目描述:某学校对教师每个月工资的计算规定如下:固定工资加课时补贴,教授的固定工资为5000元,每个课时补贴为50元;副教授的固定工资为3000元,每个课时补贴为30元;讲师的固定工资为2000元,每个课时补贴为20元。定义教师抽象类,派生教授、副教授和讲师类,编写程序求若干个教师的月工资。用C++
时间: 2023-12-10 19:41:51 浏览: 140
以下是实现该题目的示例代码:
```c++
#include <iostream>
using namespace std;
// 教师抽象类
class Teacher {
public:
virtual void calculateSalary() = 0;
virtual ~Teacher() {}
protected:
double fixedSalary; // 固定工资
double hourCost; // 每个课时补贴
int hourCount; // 课时数
};
// 教授类
class Professor : public Teacher {
public:
Professor(int hourCount) {
fixedSalary = 5000;
hourCost = 50;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "教授的月工资为:" << salary << "元" << endl;
}
};
// 副教授类
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(int hourCount) {
fixedSalary = 3000;
hourCost = 30;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "副教授的月工资为:" << salary << "元" << endl;
}
};
// 讲师类
class Lecturer : public Teacher {
public:
Lecturer(int hourCount) {
fixedSalary = 2000;
hourCost = 20;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "讲师的月工资为:" << salary << "元" << endl;
}
};
int main() {
// 创建教授对象,计算月工资
Professor p(80);
p.calculateSalary();
// 创建副教授对象,计算月工资
AssociateProfessor ap(60);
ap.calculateSalary();
// 创建讲师对象,计算月工资
Lecturer l(40);
l.calculateSalary();
return 0;
}
```
输出结果如下:
```
教授的月工资为:9000元
副教授的月工资为:4800元
讲师的月工资为:3000元
```
阅读全文