0

this is a program about payroll but when compile the program it display error message. getting error while compiling the program dev c++. 7 9 [Error] 'double Employee::taxRate' is private. [Error] no matching function for call to 'HourlyEmp::HourlyEmp(const char [8], double, int, int)'

may any one please help me?

#include <iostream>
using namespace std;

class Employee {
    private:
        string name;
        double taxRate;
    public:
        Employee( string&, double );
        string getName();
        virtual double calcSalary() = 0;
};

Employee::Employee( string& n, double tr ): name(n){
    taxRate = tr;
} 

string Employee::getName() {
    return name;
}

class SalariedEmp : public Employee
{
    private:
        double salary;
    public:
        SalariedEmp(string&,double,double);
        virtual double calcSalary();
};

SalariedEmp::SalariedEmp(string& n, double tr, double sal) : Employee( n, tr ) {
    salary = sal;
}

double SalariedEmp::calcSalary() {
    double tax = salary * taxRate;
    return salary - tax;
} 

class HourlyEmp : public Employee {
    private:
        int hours;
        double hourlyRate;
    public:
        HourlyEmp(string&,double,int,double);
        virtual double calcSalary();
};

HourlyEmp ::HourlyEmp( string& n, double tr, int h, double hr ) : Employee( n, tr) {
    hours = h;
    hourlyRate = hr;
}
double HourlyEmp::calcSalary()
{
    double grossPay, tax;
    grossPay = hours * hourlyRate;
    tax = grossPay * taxRate;
    return grossPay - tax;
} 

class CommEmp : public Employee
{
    private:
        double sales;
        double commRate;
    public:
        CommEmp( string&, double, double, double );
        virtual double calcSalary();
};

CommEmp::CommEmp( string& n, double tr, double s, double cr ) : Employee(n, tr ) {
    sales = s;
    commRate = cr;
}

double CommEmp::calcSalary()
{
    double grossPay = sales * commRate;
    double tax = grossPay * taxRate;
    return grossPay - tax;
} 

int main() {
    Employee* emp[10];
    emp[0] = new SalariedEmp( "Aamir", 0.05, 15000 );
    emp[1] = new HourlyEmp( "Faakhir", 0.06, 160, 50 );
    emp[2] = new CommEmp( "Fuaad", 0.04, 150000, 10 );
    generatePayroll( emp, 10 );
    return 0;
} 
void generatePayroll(Employee* emp[], int size) {
    cout << "Name\tNet Salary\n\n";
    for (int i = 0; i < size; i++) {
        cout << emp[i]->getName() << "\t" << emp[i]->calcSalary() << "\n";
    }
} 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
azhar
  • 351
  • 3
  • 13
  • 2
    The error message is correct. Maybe you want to make the data members protected. – drescherjm Jun 21 '21 at 18:59
  • 1
    Derived classes can only see `protected` and `public` members, not `private` from the base class. – Cory Kramer Jun 21 '21 at 18:59
  • 1
    You may want `const string& n` instead of `string& n` for the first argument of `HourlyEmp::HourlyEmp`. – MikeCAT Jun 21 '21 at 19:00
  • 1
    As the error clearly says, the variable `Employee::taxRate` is private hence it cannot be accessed outside Employee class. You are trying to access it from the inherited class SalariedEmp which will not work. In order to do that you need to define it as `protected` – Beshambher Chaukhwan Jun 21 '21 at 19:04
  • thanks a lot. it resolved after making taxRate as protected – azhar Jun 21 '21 at 19:16
  • but now i am getting an other error. [Error] no matching function for call to 'SalariedEmp::SalariedEmp(const char [4], double, double)' – azhar Jun 21 '21 at 19:16
  • 1
    @azhar if you have a new question, please [post a new question](https://stackoverflow.com/questions/ask). – Drew Dormann Jun 21 '21 at 19:25

0 Answers0