Need Assistance wtih Using a Pointer to an Object in a Program
DiggBlinkRedditDeliciousTechnorati
question by cboy++ | Moderate
I am a C++ newbie and I have put together the basics of - hourly and salary based
classes for an employee payroll. Please look at what I have so far and give me some pointers(pun intended).
The goal is to use polymorphism to compute the salary for the employees below. I am finding
it a tad bit difficult to get this to work. Any help would will be greatly appreciated!
Thanks cboy++
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class payroll{
ifstream fin;
char employeeid[12];
char employeename[20];
char maritalstatus;
int hoursworked,overtime;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void printheadings();
void printdata();
virtual float payroll() = 0;//new code used in public class as a virtual method
public: payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open("c:/dev-cpp/payroll.dat"); }//CONSTRUCTOR
payroll::~payroll(){
fin.close(); }//DESTRUCTOR
class Employee {
public: virtual void calculatenetpay ();//new code for employee classes
};
class SalariedEmployee: public Employee{
public: virtual void calculatenetpay ();
};
class HourlyEmployee: public Employee{
public: virtual void calculatenetpay ();//new code for employee classes
};
void payroll::calculategrosspay(){
if(hoursworked > 40){
overtime = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtime * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay; }//IF
else grosspay = hoursworked * hourlyrate; }//CALCULATEGROSSPAY
void payroll ::calculatetax(){
if(grosspay >= 500) taxrate = .30;
else if(grosspay > 200.00) taxrate = .20;
else taxrate = .10;
if(maritalstatus == 'S' || maritalstatus == 's')
taxrate = taxrate + .05;
taxamount = grosspay * taxrate; }//CALCULATETAX
void payroll :: calculatenetpay(){
netpay = grosspay - taxamount; }//CALCULATENETPAY
void payroll::printheadings(){
cout<<setw(45)<<"--PAYROLL REPORT--" <<endl;
cout<<"------------------------------------------------------------------"
<<endl;
cout<<" NAME ID HW OT RT-PAY OT-PAY GROSS TAX NETPAY" <<endl;
cout<<"------------------------------------------------------------------"
<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
<<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
<<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
<<netpay<<endl; }//PRINTDATA
void payroll::printreport(){
int i = 0;
printheadings();
while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
i++; }//WHILE
}//PRINTREPORT
int main(){
payroll employee;
employee.printreport();
SalariedEmployee salemp;
HourlyEmployee houremp;
Employee *p0 = new &salemp;
Employee *p1 = new &houremp;
p0->calculatenetpay(); // calls SalariedEmployee::calculatenetpay()
p1->calculatenetpay(); // calls HourlyEmployee::calculatenetpay()
cout << salemp.calculatenetpay() << endl;
cout << houremp.calculatenetpay() << endl;
system ("PAUSE");
return 0;
}//MAIN
This is my datafile info below - name, social s., married or single, hoursworked, hourly rate:
Garcia 097-45-5678 M 45 10.00
Mary 078-78-6754 S 60 15.50
Ramon 078-56-5645 M 58 16.99
John 079-56-4577 S 25 6.99
Billy 692-03-5986 M 31 11.00
Post reply
Subscriptions
Got a C++ Question?
Just Sign Up and ask the top C++ experts!
|