作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我类(class)的作业。我应该有两个类——带薪类和按小时类——都需要从另一个类(class)员工那里继承。当我编译我的代码时,我没有得到任何错误,但是当我运行我的程序时,只有 header 和 netpay 函数产生任何输出。我知道这段代码的大部分都在运行,因为如果我删除新类(class)(带薪和按小时计费),程序就可以正常工作。我很难理解继承,我做错了什么,而且我似乎无法弄清楚。任何帮助都会很棒,谢谢!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
using namespace std;
class employee{
protected:
ifstream fin;
char firstname;
char lastname;
int employeeid, inputs, hours, overtimehours, paymenttype;
double rate, overtimerate, grosspay, overtimepay, netpay, netpayavg, taxamount, taxrate;
double sum = 0.0;
void findinputs();
double virtual findgrosspay();
void findtaxamount();
void findnetpay();
void findnetpayavg();
void tableheader();
void outputdata();
public: void setvariables(int empid, double otp, double oth, char fname, char lname, int paytype, double hourlyrate, double salary, double hrs){
employeeid = empid;
firstname = fname;
lastname = lname;
hours = hrs;
overtimehours = oth;
overtimepay = otp;
paymenttype = paytype;
if(paytype == 1){
salary = rate;
else
hourly = rate;
}
}
public: employee();
~employee();
void printdata();
}; //base class
employee::employee(){
fin.open("M6employee.in");
}
employee::~employee(){
fin.close();
}
class salaried : public employee{
public: double findgrosspay(double hrs, double salary, double otp, double oth){
salary = (salary / 52) / 40;
if ((hrs - 40) > 0 ){
oth = hrs - 40;
}
else{
oth = 0;
} // this program finds gross pay of salaried employee
otp = oth * (salary * 1.5);
grosspay = (hrs * salary) + otp;
return grosspay;
cout << "your salary is :" << grosspay << endl;
} // supposed to inherit from employee
};
class hourly : public employee{
public: double findgrosspay(double hrs, double hourlyrate, double otp, double oth){
if ((hrs - 40) > 0 ){
oth = hrs - 40;
}
else{
oth = 0;
} // this function should find grosspay of hourly employee
otp = oth * (hourlyrate * 1.5);
grosspay = (hourlyrate * hourlyrate) + otp;
return grosspay;
cout << "Your hourly rate is :" << grosspay << endl;
}// supposed to inherit from employee
};
main(){
int j;
char firstname[j], lastname[j];
int hours[j];
double rate[j], netpay[j];
employee employeedata;
employeedata.printdata();
return 0;
}
double employee::findgrosspay(){
}
void employee::findtaxamount(){
taxrate = .30;
taxamount = grosspay * taxrate;
cout << " Your taxamount is :" << taxamount << endl;
}
void employee::findnetpay(){
netpay = grosspay - taxamount;
sum = netpay + sum;
cout << "Your netpay is :" << netpay << endl;
}
void employee::findnetpayavg(){
netpayavg = sum / inputs;
cout << endl << endl;
cout << "The netpay average is : " << netpayavg << endl;
} // find average of employee netpay
void employee::tableheader(){
cout <<"JAMES MANN'S PAYROLL PROGRAM" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << "Employee Name Hours Hourly Rate Overtime Pay Gross Pay Tax Amount Net Pay" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
} // creates header for table
void employee::outputdata(){
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
cout << setw(6) << firstname << setw(8) << lastname << setw(6) << hours << setw(13) << rate << setw(12) << overtimepay
<< setw(12) << grosspay << setw(10) << taxamount << setw(10) << netpay << endl;
} // outputs calculations from other functions
void employee::findinputs(){
inputs = 0;
string lines;
while(std::getline(fin, lines))
++inputs;
fin.close();
fin.open("M6employee.in");
cout << "Your input total is :" << endl;
} //used to count the number of inputs from file
void employee::printdata(){
tableheader();
findinputs();
while(fin >> firstname >> lastname >> hours >> rate >> paymenttype){
findgrosspay();
findtaxamount();
findnetpay();
outputdata();
}
findnetpayavg();
} // used to print the the results of program
最佳答案
virtual
函数在派生类中被覆盖时需要具有相同的签名,因此 findgrosspay();
需要
double virtual findgrosspay(double, double, double, double);
您可以通过在派生类中使用 override
关键字来强制执行此操作,编译器会告诉您这个问题
# in salaried
double findgrosspay(double hrs, double salary, double otp, double oth) override;
# in hourly
double findgrosspay(double hrs, double hourlyrate, double otp, double oth) override;
关于c++ - 我在理解继承和让多个类协同工作时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763978/
我想插入 备注 关于要在我的 latex 文档的特定位置进行的修复 也许有一个 列表的“待办事项/修复我” 你怎么处理这个? 似乎一种方法是使用 fixme 包,但我无法使其工作。 有人在用吗? 最佳
错误:无法创建表。我已经创建了一个数据库,并且已经提供了所有特权。但仍然无法登录协作模块。我受够了,但我不想放弃。我已经尝试了所有可能的方法,但都行不通。 Stackoverflow 是我所知道的最好
我是一名优秀的程序员,十分优秀!