某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元, 每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。c++
时间: 2024-03-24 16:39:54 浏览: 166
基于ASP+SqlServer某高校工资管理系统的设计与实现(源代码+论文)
好的,以下是用 C++ 实现该程序的代码示例:
```cpp
#include <iostream>
using namespace std;
// 定义教师抽象类
class Teacher {
public:
virtual double get_salary(int course_num) = 0;
};
// 定义不同职称的教师类
class Professor : public Teacher {
public:
Professor(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
class Lecturer : public Teacher {
public:
Lecturer(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
// 计算若干教师的月工资
int main() {
Teacher* teacher_list[5] = {
new Professor(5000, 50),
new AssociateProfessor(3000, 30),
new Lecturer(2000, 20),
new Professor(5000, 50),
new Lecturer(2000, 20)
};
for (int i = 0; i < 5; i++) {
int course_num;
cout << "请输入" << typeid(*teacher_list[i]).name() << "授课的课时数:";
cin >> course_num;
double salary = teacher_list[i]->get_salary(course_num);
cout << typeid(*teacher_list[i]).name() << "的月工资为:" << salary << "元" << endl;
}
return 0;
}
```
程序运行结果示例:
```
请输入class Professor的月工资为:50授课的课时数:20
class Professor的月工资为:6000元
请输入class AssociateProfessor的月工资为:30授课的课时数:15
class AssociateProfessor的月工资为:4500元
请输入class Lecturer的月工资为:20授课的课时数:10
class Lecturer的月工资为:2200元
请输入class Professor的月工资为:50授课的课时数:30
class Professor的月工资为:6500元
请输入class Lecturer的月工资为:20授课的课时数:12
class Lecturer的月工资为:2440元
```
阅读全文