c++ - 我在理解继承和让多个类协同工作时遇到问题?

标签 c++ inheritance polymorphism

这是我类(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/

相关文章:

c++ - 从类模板派生时使用派生类

c++ - 如何在多态性中为指定类型分配内存?

r - "matrix-like?"的(精确)含义是什么

python - 子类构造函数抛出 TypeError : __init__() takes 2 positional arguments but 5 were given

c++ - 从多态指针检查类成员是否存在?

c++ - 如何获取所有子节点值

c++ - 我怎样才能实现一个通用的最小值?

c++ - 由于它们的大 O 复杂性,两个函数没有花费我预期的时间,谁能解释为什么?

c++ - 从 Map 中获取 Value 中具有给定值的元素

java - 在 Java 中添加实例变量后静态方法/变量的行为发生变化