Ok, I never really understood vectors and I have write a program that uses a vector of Employee references to store the company’s payroll. My program will load the payroll by reading a file (employee.dat) that contains one employee’s information per line. Each line of the file will begin with a letter (S for salaried, H for hourly, C for commission, or B for base plus commission.) The fields on the remainder of the line will depend on the first letter of the line and will be separated by spaces. I didn't think you could use a vector of references but here is the code. Also, I need to know how to read it in from a file and to use the appropriate object. Should I use switch statements or else ifs? Remember, its just modifying it.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include <vector>
using std::vector;
// include definitions of classes in Employee hierarchy
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
int main()
{
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", 800 );
HourlyEmployee hourlyEmployee(
"Karen", "Price", "222-22-2222", 16.75, 40 );
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
// create vector of four base-class pointers
vector < Employee * > employees( 4 );
// initialize vector with Employees
employees[ 0 ] = &salariedEmployee;
employees[ 1 ] = &hourlyEmployee;
employees[ 2 ] = &commissionEmployee;
employees[ 3 ] = &basePlusCommissionEmployee;
return 0;
} // end main
These are all in there own header file associated with there name.
Employee( const string &, const string &, const string & );
//constructor definition
Employee::Employee( const string &first, const string &last,
const string &ssn )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn )
{
// empty body
} // end Employee constructor
HourlyEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0 );
// constructor definition
HourlyEmployee::HourlyEmployee( const string &first, const string &last,
const string &ssn, double hourlyWage, double hoursWorked )
: Employee( first, last, ssn )
{
setWage( hourlyWage ); // validate hourly wage
setHours( hoursWorked ); // validate hours worked
} // end HourlyEmployee constructor
SalariedEmployee( const string &, const string &,
const string &, double = 0.0 );
// constructor definition
SalariedEmployee::SalariedEmployee( const string &first,
const string &last, const string &ssn, double salary )
: Employee( first, last, ssn )
{
setWeeklySalary( salary );
} // end SalariedEmployee constructor
CommissionEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0 );
// constructor defintion
CommissionEmployee::CommissionEmployee( const string &first,
const string &last, const string &ssn, double sales, double rate )
: Employee( first, last, ssn )
{
setGrossSales( sales );
setCommissionRate( rate );
} // end CommissionEmployee constructor
BasePlusCommissionEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0, double = 0.0 );
//constructor definition
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor