Hung up on error

Very close to completing this assignment. Had the program compiling without errors when the instructor suggested I try using dynamic binding. Ran into error: 109 C:\Dev-Cpp\Pay16.cpp expected primary-expression before '->' token". Can anyone please explain? Code follows:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class payroll {
ifstream fin;
public: char employeename[14], maritalstatus;
int employeeid, n;
float taxrate;
double hoursworked, overtimehours, regularhours;
double hourlyrate, regularpay, totalnetpay, annualpay;
double taxamount, avgnetpay, netpay, grosspay, overtimepay;
virtual void findgrosspay();
void findtaxamount();
void findnetpay();
void printheaders();
void printdata();
void printavgnet();
payroll();
~payroll();
void printreport(); };

class salaried : public payroll {
public: void findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
if (hoursworked > 0) {
overtimepay = hoursworked * (regularpay / 52 / 40);
}//If
regularpay = hourlyrate / 52;
grosspay = regularpay + overtimepay;
} };

class hourly : public payroll {
public: void findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
if (hoursworked > 40 ) {
overtimehours = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtimehours * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay; }
else
grosspay = hoursworked * hourlyrate;
regularpay = grosspay;
} };

payroll::payroll() {
fin.open("payroll.dat"); }
payroll::~payroll() {
fin.close(); }

void payroll::findgrosspay() {
overtimehours = 0;
regularpay = 0;
overtimepay = 0;
grosspay = 0;
if (hoursworked > 40 ) {
overtimehours = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtimehours * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay; }
else
grosspay = hoursworked * hourlyrate;
regularpay = grosspay;
}// findgrosspay

void payroll::findtaxamount() {
taxrate = .30;
taxamount = grosspay * taxrate;
}// findtaxamount

void payroll::findnetpay() {
netpay = grosspay - taxamount;
totalnetpay = totalnetpay + netpay;
}// findnetpay

void payroll::printavgnet() {
avgnetpay = totalnetpay / n;
cout<<endl<<"The average net pay for "<<n <<" employees is "
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<avgnetpay<<endl;
}// printavgnet

void payroll::printheaders() {
cout<<setw(45)<<"-PAYROLL REPORT-"<<endl<<endl;
cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS"
" TAX NETPAY"<<endl;
cout<<"-----------------------------------------------------------------"<<endl;
}//printheaders

void payroll::printdata() {
cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
<<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
<<taxamount<<setw(8)<<netpay<<endl;
}//printdata

void payroll::printreport() {
n = 0;
printheaders();
while (fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate) {
if (employeeid < 9000)
hourly->findgrosspay();
else
salaried->findgrosspay();
findtaxamount();
findnetpay();
printdata();
n++;
}// While
printavgnet();
}// printreport

int main() {
payroll employee;
employee.printreport();
} //main
closed account (z05DSL3A)
Your base class (payroll) can not use the dirived classes (hourly and salaried).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class payroll 
{
    ifstream fin;
public: 
    char employeename[14], maritalstatus;
    int employeeid, n;
    float taxrate;
    double hoursworked, overtimehours, regularhours;
    double hourlyrate, regularpay, totalnetpay, annualpay;
    double taxamount, avgnetpay, netpay, grosspay, overtimepay;

    virtual void findgrosspay();
    void findtaxamount();
    void findnetpay();
    void printheaders();
    void printdata();
    void printavgnet();

    payroll();
    ~payroll();
    void printreport(); 
};

class salaried : public payroll 
{
public: 
    void findgrosspay() 
    {
        overtimehours = 0;
        regularpay = 0;
        overtimepay = 0;
        grosspay = 0;
        if (hoursworked > 0) 
        {
            overtimepay = hoursworked * (regularpay / 52 / 40);
        }//If
        regularpay = hourlyrate / 52;
        grosspay = regularpay + overtimepay;
    } 
};

class hourly : public payroll 
{
public: 
    void findgrosspay() 
    {
        overtimehours = 0;
        regularpay = 0;
        overtimepay = 0;
        grosspay = 0;
        if (hoursworked > 40 ) 
        {
            overtimehours = hoursworked - 40;
            regularpay = hoursworked * hourlyrate;
            overtimepay = overtimehours * (hourlyrate * 1.5);
            grosspay = regularpay + overtimepay; 
        }
        else
            grosspay = hoursworked * hourlyrate;
        regularpay = grosspay; 
    } 
};

payroll::payroll() 
{
    fin.open("payroll.dat"); 
}

payroll::~payroll() 
{
    fin.close(); 
}

void payroll::findgrosspay() 
{
    overtimehours = 0;
    regularpay = 0;
    overtimepay = 0;
    grosspay = 0;
    if (hoursworked > 40 ) 
    {
        overtimehours = hoursworked - 40;
        regularpay = hoursworked * hourlyrate;
        overtimepay = overtimehours * (hourlyrate * 1.5);
        grosspay = regularpay + overtimepay; 
    }
    else
        grosspay = hoursworked * hourlyrate;
    regularpay = grosspay; 
}// findgrosspay

void payroll::findtaxamount() 
{
    taxrate = .30;
    taxamount = grosspay * taxrate;
}// findtaxamount

void payroll::findnetpay() 
{
    netpay = grosspay - taxamount;
    totalnetpay = totalnetpay + netpay;
}// findnetpay

void payroll::printavgnet() 
{
    avgnetpay = totalnetpay / n;
    cout<<endl<<"The average net pay for "<<n <<" employees is "
    <<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<avgnetpay<<endl;
}// printavgnet

void payroll::printheaders() 
{
    cout<<setw(45)<<"-PAYROLL REPORT-"<<endl<<endl;
    cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS"
    " TAX NETPAY"<<endl;
    cout<<"-----------------------------------------------------------------"<<endl; 
}//printheaders

void payroll::printdata() 
{
    cout<<setw(10)<<left<<employeename<<setw(4)<<employeeid;
    cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
    cout<<setw(6)<<right<<hoursworked<<setw(5)<<overtimehours<<setw(8)
    <<regularpay<<setw(8)<<overtimepay<<setw(8)<<grosspay<<setw(8)
    <<taxamount<<setw(8)<<netpay<<endl; 
}//printdata

void payroll::printreport() 
{
    n = 0;
    printheaders();
    while (fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate) 
    {
        if (employeeid < 9000) 
            hourly->findgrosspay();
        else
            salaried->findgrosspay();
        findtaxamount();
        findnetpay();
        printdata();
        n++;
    }// While
    printavgnet();
}// printreport

int main() 
{
    payroll employee;
    employee.printreport();
} //main 

Last edited on
I'm guessing that the problem is both of the following lines
 
hourly->findgrosspay();

and
 
salaried->findgrosspay();

What you've done, or rather not done is create an instance of hourly or salaried. By calling their members from within the parent class you've made things quite complicated to fix as Grey Wolf has already pointed out, a parent cannot call methods fom it's derived classes, it should be the other way around. You call parent methods from the derived class, as, by creating an instance of the child, you automatically create an instance of the parent .

You probably need to override printreport() in each of hourly and salaried. to make this work

closed account (z05DSL3A)
I am thinking that you would have to split the responsibilities of the payroll class. Move the thing to do with hoursworked etc. into a different class that can be a base class for salaried and hourly. payroll would then become a container for the new base class.

Hope that makes sense.
Topic archived. No new replies allowed.