THE ZEPINT NETWORK

programmer assist

C++ C++ XML Feeds

C++ Questions C++ Solutions C++ Articles

C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating large-scale applications. C++ is a superset of the C language. A related programming language, Java, is based on C++ but optimized for the distribution of program objects in a network such as the Internet. Java is somewhat simpler and easier to learn than C++ and has characteristics that give it other advantages over C++. However, both languages require a considerable amount of study.

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!

Search via Google

User Login

Email Address

Password

C++ Experts

Rank Expert Points
#1 Support@PA 500
This a list of the Top C++ experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?