C++ 我在理解继承和让多个类一起工作时遇到问题?

C++ 我在理解继承和让多个类一起工作时遇到问题?,c++,inheritance,polymorphism,C++,Inheritance,Polymorphism,这是我们班的作业。我应该有两个类——工薪和小时制——它们都需要从另一个类雇员继承。当我编译我的代码时,我没有得到任何错误,但是当我运行我的程序时,只有header和netpay函数产生任何输出。我知道这段代码的大部分都在运行,因为如果我删除了新的类(工资类和小时类),程序就可以正常运行。我很难理解继承,我做错了什么,但我似乎无法理解。任何帮助都会很好,谢谢 #include <iostream> #include <iomanip> #include <fstrea

这是我们班的作业。我应该有两个类——工薪和小时制——它们都需要从另一个类雇员继承。当我编译我的代码时,我没有得到任何错误,但是当我运行我的程序时,只有header和netpay函数产生任何输出。我知道这段代码的大部分都在运行,因为如果我删除了新的类(工资类和小时类),程序就可以正常运行。我很难理解继承,我做错了什么,但我似乎无法理解。任何帮助都会很好,谢谢

#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
#包括
#包括
#包括
#包括
使用名称空间std;
类雇员{
受保护的:
流鳍;
姓名;
姓氏字符;
int employeeid、输入、小时数、超时小时数、paymenttype;
双倍税率、超期税率、grosspay、超期工资、网付、网付平均税率、税额、税率;
双和=0.0;
void findinput();
双虚拟findgrosspay();
void findtaxamount();
作废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;
小时=小时;
超时小时=其他时间;
超期支付=otp;
paymenttype=paytype;
如果(工资类型==1){
工资=费率;
其他的
每小时=费率;
}
}
public:employee();
~employee();
void printdata();
}; //基类
雇员::雇员(){
财务公开(“M6employee.in”);
}
雇员::~employee(){
fin.close();
}
受薪类别:公职人员{
公众:双倍findgrosspay(双倍工时、双倍工资、双倍otp、双倍oth){
薪金=(薪金/52)/40;
如果((小时-40)>0){
oth=小时-40;
}
否则{
oth=0;
}//此程序查找受薪员工的工资总额
otp=oth*(工资*1.5);
格罗斯佩=(小时*工资)+otp;
返回格罗斯佩;

cout
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;

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;

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;

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;


因为格式有点差,代码根本不是最小值,所以被否决。因为格式有点差,代码根本不是最小值,所以被否决。因为格式有点差,代码根本不是最小值,所以被否决。因为格式有点差,代码根本不是最小值,所以被否决。感谢您的快速回复响应。我尝试了此操作,现在得到以下错误:错误]在'{' token@James在你所有的类定义之后,你缺少了一个
。我只是仔细检查了我的代码,我确实有。你看了这篇文章,看起来它们也在那里。我为格式错误道歉,我是新来编码的!;)如果这个答案满足了你现有的问题,那么问一个新的问题就更合适了w询问您是否遇到新错误,并尝试将代码精简为显示错误的最小相关代码段。感谢您的快速响应。我尝试了此操作,现在得到以下错误:错误]之前预期的非限定id'{' token@James在你所有的类定义之后,你缺少了一个
。我只是仔细检查了我的代码,我确实有。你看了这篇文章,看起来它们也在那里。我为格式错误道歉,我是新来编码的!;)如果这个答案满足了你现有的问题,那么问一个新的问题就更合适了w询问您是否遇到新错误,并尝试将代码精简为显示错误的最小相关代码段。感谢您的快速响应。我尝试了此操作,现在得到以下错误:错误]之前预期的非限定id'{' token@James在你所有的类定义之后,你缺少了一个
。我只是仔细检查了我的代码,我确实有。你看了这篇文章,看起来它们也在那里。我为格式错误道歉,我是新来编码的!;)如果这个答案满足了你现有的问题,那么问一个新的问题就更合适了w询问您是否遇到新错误,并尝试将代码精简为显示错误的最小相关代码段。感谢您的快速响应。我尝试了此操作,现在得到以下错误:错误]之前预期的非限定id'{' token@James在你所有的类定义之后,你缺少了一个
。我只是仔细检查了我的代码,我确实有。你看了这篇文章,看起来它们也在那里。我为格式错误道歉,我是新来编码的!;)如果这个答案满足了你现有的问题,那么问一个新的问题就更合适了问一下,你是否明白了