某学校教职工的工资计算方法为:
所有教职工都有基本工资。
教师月工资为基本工资+课时补贴,课时补贴根据职称和课时计算。例如,每课时教授补贴50元,副教授补贴30元,讲师补贴20元。
麻烦看一下为什么总是这样显示,刚学不久
//staff.h
#ifndef STAFF_H
#define STAFF_H
class staff
{
public:
staff(const char* );
virtual ~staff();
virtual void print() const;
private:
char name[20];
};
#endif // STAFF_H
//staff.cpp
#include "staff.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
staff::staff(const char* str)
{
strcpy_s(name,20,str);
}
void staff::print() const
{
cout<<name;
}
staff::~staff()
{
//dtor
}
//teacher.h
#include<cstring>
class teacher:public staff
{
public:
teacher(const char* ,double,int=0 );
~teacher();
double setSalary(double);
int setTime(int);
virtual void print() const;
private:
double title;
int time;
float s;
};
#endif // TEACHER_H
//teacher.cpp
#include "teacher.h"
#include "staff.h"
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
teacher::teacher(const char* str,double t,int t1):staff(str)
{
setSalary(t);
setTime(t1);
}
int teacher::setTime(int t1)
{
time=t1;
}
double teacher::setSalary(double t)
{
title=t;
if (title=1)
{
s=3000+(50*time);
}
else if(title=2)
{
s=3000+(30*time);
}
else if(title=3)
{
s=3000+(20*time);
}
else
{
s=3000;
}
return s;
}
void teacher::print() const
{
staff::print();
cout<<s;
}
teacher::~teacher()
{
//dtor
}
//main.cpp
#include "teacher.h"
#include "staff.h"
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
void test01()
{
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
teacher t1("张三",1,1);
staff *basePtr;
basePtr=&t1;
basePtr->print();
}
int main()
{
system("chcp 65001");
cout << "2224240742-庞雨桐" << endl;
test01();
}
这段代码存在一些问题,我来帮您逐一解决:
在teacher类中,setTime函数返回值类型为int,但是没有返回值,应该将int改为void;
在teacher类中,setSalary函数的if判断中使用了赋值操作符"=",这是错误的,应该使用相等操作符"==";
在setSalary函数中,计算教师月薪的代码应该放在if语句块里面,具体如下:
if (title==1)
{
s=3000+(50*time);
}
else if(title==2)
{
s=3000+(30*time);
}
else if(title==3)
{
s=3000+(20*time);
}
else
{
s=3000;
}
修复上述问题以后,可以将main函数改成如下:
int main()
{
system("chcp 65001");
cout << "2224240742-庞雨桐" << endl;
test01();
return 0;
}
这样就能顺利运行了。