I am having problems understanding inheritance and getting multiple classes to work together?

81 Views Asked by At

This is an assignment for my class. I am supposed to have two classes--salaried and hourly-- which both need to inherit from another class employee. When I compile my code I get no errors but when I run my program, only the header and the netpay functions produce any output. I know the majority of this code is functioning because if I remove the new classes(salaried and hourly) the program works fine. I am having a hard time understanding inheritance, what I am doing wrong, and I can't seem to figure it out. Any help would be great, Thanks!

#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

There are 1 best solutions below

4
On BEST ANSWER

virtual functions need to have the same signature when they are intended to be overriden in the derived classes, so findgrosspay(); needs to be

double virtual findgrosspay(double, double, double, double);

You can enforce this by using the override keyword in your derived classes, and the compiler would have told you this issue

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