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

时间:2015-06-10 18:04:34

标签: c++ inheritance polymorphism

这是我班级的作业。我应该有两个班级 - 薪水和小时 - 这两个班级都需要从另一个班级员工继承。当我编译我的代码时,我没有错误,但是当我运行我的程序时,只有标题和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

1 个答案:

答案 0 :(得分:3)

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;