Does anyone know how i can improve this code. specifically my constructor.
Code:
#include <iostream>
using namespace std;
class netpay
{
private:
const double PAYRATE;
double ssTaxRate, fedTaxRate, stateTaxRate;
double netPay, grossPay, ssTax, fedTax, stateTax, healthIns;
int unionDue, hours, dependants;
public:
//my horrible constructor
netpay() : PAYRATE(16.78), ssTaxRate(0.06), fedTaxRate(0.14),
stateTaxRate(0.05), netPay(0.0), grossPay(0.0), ssTax(0.0),
fedTax(0.0), stateTax(0.0), healthIns(0.0), unionDue(10),
hours(0), dependants(0)
{}
void getinfo();
void calnet();
void showinfo();
};
void netpay:: getinfo()
{
do
{
cout << "Enter the number of hours worked: "<<endl;
cin >> hours;
}while (hours <= 0);
do
{
cout << "Enter the number of dependants: "<<endl;
cin >> dependants;
}while (dependants < 0);
if (dependants >= 3)
healthIns = 35;
else
healthIns = 0;
}
void netpay:: calnet() // calculates the netpay
{
if (hours > 40)
grossPay = PAYRATE * 40 + 1.5 * PAYRATE * (hours - 40);
else
grossPay = hours * PAYRATE;
ssTax = grossPay * ssTaxRate;
fedTax = grossPay * fedTaxRate;
stateTax = grossPay * stateTaxRate;
netPay = grossPay - (ssTax + fedTax + stateTax + healthIns + unionDue);
}
void netpay:: showinfo() // outputs all of the the users information
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Your grosspay was: "<< grossPay <<endl;
cout << "Your social security tax was: " << ssTax <<endl;
cout << "Your federal income tax was: " << fedTax <<endl;
cout << "Your State income tax was: " << stateTax <<endl;
cout << "Your union dues are: " << unionDue <<endl;
cout << "Your health insurance was: " << healthIns <<endl;
cout << "Your netpay was: " << netPay << endl;
}
int main()
{
char ans('n');
do
{
netpay n1; // create object
cout << "This program will calculate your netpay. " <<endl;
n1.getinfo(); //these are
n1.calnet(); // the function
n1.showinfo(); // calls
cout << "Would you like to input again(y/n)"<<endl;
cin >> ans;
}while (ans == 'y' || ans == 'Y');
return 0;
}
NOTE: if you notice any other things that i can improve on plz tell me.