用赋值兼容规则改写#include <iostream> #include <string> using namespace std; // 定义教师抽象类 class Teacher { public: Teacher(string name, int course_num) : name_(name), course_num_(course_num) {} virtual ~Teacher() {} virtual int get_salary() = 0; // 计算月工资的虚函数 virtual string get_name() = 0;//获取老师名字 protected: string name_; // 姓名 int course_num_; // 课时数 }; // 定义教授类 class Professor : public Teacher { public: Professor(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 5000 + course_num_ * 50; } virtual string get_name() { return name_; } }; // 定义副教授类 class AssociateProfessor : public Teacher { public: AssociateProfessor(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 3000 + course_num_ * 30; } virtual string get_name() { return name_; } }; // 定义讲师类 class Lecturer : public Teacher { public: Lecturer(string name, int course_num) : Teacher(name, course_num) {} // 计算月工资的具体实现 virtual int get_salary() { return 2000 + course_num_ * 20; } virtual string get_name() { return name_; } }; int main() { // 创建教授对象 Professor p("好小子", 10); cout << "教授 " << p.get_name() << " 的工资是 " << p.get_salary() << " 元." << endl; // 创建副教授对象 AssociateProfessor ap("好小伙", 20); cout << "副教授 " << ap.get_name() << " 的工资是" << ap.get_salary() << " 元." << endl; // 创建讲师对象 Lecturer l("好家伙", 30); cout << "讲师 " << l.get_name() << " 的工资是 " << l.get_salary() << " 元." << endl; system("pause"); return 0; }
时间: 2024-03-17 17:40:56 浏览: 57
将#include <iostream>和#include <string>改为赋值兼容规则,则可以改为以下代码:
```
#include<iostream.h>
#include<string.h>
using namespace std;
class Teacher {
public:
Teacher(char* name, int course_num) : name_(name), course_num_(course_num) {}
virtual ~Teacher() {}
virtual int get_salary() = 0;
virtual char* get_name() = 0;
protected:
char* name_;
int course_num_;
};
class Professor : public Teacher {
public:
Professor(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 5000 + course_num_ * 50;
}
virtual char* get_name()
{
return name_;
}
};
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 3000 + course_num_ * 30;
}
virtual char* get_name()
{
return name_;
}
};
class Lecturer : public Teacher {
public:
Lecturer(char* name, int course_num) : Teacher(name, course_num) {}
virtual int get_salary()
{
return 2000 + course_num_ * 20;
}
virtual char* get_name()
{
return name_;
}
};
void main() {
Professor p("好小子", 10);
cout << "教授 " << p.get_name() << " 的工资是 " << p.get_salary() << " 元." << endl;
AssociateProfessor ap("好小伙", 20);
cout << "副教授 " << ap.get_name() << " 的工资是" << ap.get_salary() << " 元." << endl;
Lecturer l("好家伙", 30);
cout << "讲师 " << l.get_name() << " 的工资是 " << l.get_salary() << " 元." << endl;
system("pause");
}
```
阅读全文