I had to modify this code to inlcude a const. When I did that, I keep getting three errors.
This is my code,
1. what am I doing wrong?
2. I'm suppose to have the empId left justified and the pay right justified. How do I do both of them?
Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
//Updating lass to calculate pay for an Employee.
// Class definition.
class Employee
{
private:
int _empId; // Employee Id.
const double _payRate; // Rate of Pay constant
// double _hoursWorked; // Hours Worked.
public:
Employee( ); // Constructor.
Employee( int empId, double payRate ); // Overloaded Constructor.
double calculatePay( int hoursWorked ); // Calculates the Employees pay.
double salary;
void setEmpId( int empId ); // Mutator Method - allows you to set the employee id.
int getEmpId( ); // Accessor Method - allows you to get the employee id.
};
// Constructor.
Employee::Employee( )
{
this->_empId = 1;
this->_payRate = 5.33;
// this->_hoursWorked = 10.0;
}
Employee::Employee( int empId )
{
this->_empId = empId;
}
// Calculates the Employee's pay based upon the _payRate and _hoursWorked.
double Employee::calculatePay( int hoursWorked )
{
return this->_payRate * hoursWorked;
}
// Sets the Employee Id
void Employee::setEmpId( int empId )
{
this->_empId = empId;
}
// Gets the employee id.
int Employee::getEmpId( )
{
return this->_empId;
}
// Main C++ function.
int _tmain(int argc, _TCHAR* argv[])
{
const double _payRate = 3.75;
Employee emp( 1, 12.50 );
double emp1Hours, emp2Hours, emp3Hours;
double salary = _payRate * emp1Hours;
cout << "Please enter number of hours worked: ";
cin >> emp1Hours;
cout<< "setw(5)<<setiosflags(ios::left)<<setprecision(2)<<salary<<";
cout<<"---------------------------------------------";
cout<< "Please type number of hours for second employee: ";
cin>> emp2Hours;
cout<< "setw(5)<<setiosflags(ios::left)<<setprecision(2)<<salary<<";
cout<<"---------------------------------------------";
cout<<"Please type third employee's hours worked: ";
cin>>emp3Hours;
cout<< "setw(5)<<setiosflags(ios::left)<<setprecision(2)<<salary<<";
cout<<"---------------------------------------------";
cout << "Employee " << emp.getEmpId( ) << "'s salary is : " << emp.calculatePay( 40 );
cout<<" Total is (emp1Hours + emp2Hours + emp3Hours)"<< endl;
cout << endl << endl << "Please press a key and then press enter to quit." << endl;
char a;
cin >> a;
return 0;
}
These are the errors:
error C2758: 'Employee::_payRate' : must be initialized in constructor base/member initializer list
error C2166: l-value specifies const object
error C2511: 'Employee::Employee(int)' : overloaded member function not found in 'Employee'