//某学校对教师每月工资的计算公式如下:固定工资+课时补贴。 //教授的固定工资为5000元,每个课时补贴50元; //副教授的固定工资为3000元, 每个课时补贴30元; //讲师的固定工资为2000元,每个课时补贴20元。 //定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。 #include<iostream> using namespace std; class teacher{ protected: int workhour; double salary; public: teacher(int wh=0){workhour=wh} virtual double calsalary()=0; void print(){cout<<"工资为"<<salary<<endl;} }; class professor:public teacher{ public: professor(int wh):teacher(wh){} void calsalary(){salary=5000+50*workhour;} }; class viceprofessor:public teacher{ public: viceprofessor(int wh=0):teacher(wh){} void calsalary(){salary=3000+30*workhour;} }; class lecturer:public teacher{ public: lecturer(int wh=0):teacher(wh){} void calsalary(){salary=2000+20*workhour;} }; int main(){ teacher *p; professor a(200); viceprofessor b(240); lecturer c(260); p=&a; p>-calsalary(); cout<<"professor-"<<professor.print(); p=&b; p>-calsalary(); cout<<"viceprofessor-"<<viceprofessor.print(); p=&c; p>-calsalary(); cout<<"lecturer-"<<lecturer.print(); return 0; }哪里错了,怎么改正
时间: 2024-03-22 21:38:50 浏览: 21
在teacher类中的calsalary函数的声明中,应该加上virtual关键字,表示这是一个虚函数。同时,在professor、viceprofessor、lecturer类中的calsalary函数的定义中,应该将返回值类型改为void,因为这些函数不需要返回值,而是直接修改对象的salary成员变量。
以下是修改后的代码:
```
#include<iostream>
using namespace std;
class teacher{
protected:
int workhour;
double salary;
public:
teacher(int wh=0){workhour=wh;}
virtual double calsalary()=0;
void print(){cout<<"工资为"<<salary<<endl;}
};
class professor:public teacher{
public:
professor(int wh):teacher(wh){}
virtual void calsalary(){salary=5000+50*workhour;}
};
class viceprofessor:public teacher{
public:
viceprofessor(int wh=0):teacher(wh){}
virtual void calsalary(){salary=3000+30*workhour;}
};
class lecturer:public teacher{
public:
lecturer(int wh=0):teacher(wh){}
virtual void calsalary(){salary=2000+20*workhour;}
};
int main(){
teacher *p;
professor a(200);
viceprofessor b(240);
lecturer c(260);
p=&a;
p->calsalary();
cout<<"professor-";
a.print();
p=&b;
p->calsalary();
cout<<"viceprofessor-";
b.print();
p=&c;
p->calsalary();
cout<<"lecturer-";
c.print();
return 0;
}
```
注意,还有一个错误是在调用calsalary函数时,应该用箭头运算符“->”而不是“>-”,这是一个笔误。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)