View Full Version : Array of Payroll Objects help in C++

10-16-2012, 07:48 PM
Is there any way you can pass a vector to a function, in my case the getHour function so it can be stored and later used again? I need to declare two data types. One is an int(hours) and the other is a double(wage). I have been unsuccessful trying to use a standard array to accomplish this, and the only way to go seems to be with a vector. The problem is how do use the vector to pass it on to a function like the getHour function? I have my code, and it is incomplete because I am stuck at the point where I need to pass the array as an object to the function. Here is the assignment followed by my code:

Design a PayRoll class that has data members for an employee’s hourly pay rate and
number of hours worked. Write a program with an array of seven PayRoll objects. The
program should read the number of hours each employee worked and their hourly pay
rate from a file and call class functions to store this information in the appropriate
objects. It should then call a class function, once for each object, to return the employee’s
gross pay, so this information can be displayed. Sample data to test this program can be
found in the payroll.dat file.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

class Payroll
int numHours;
double hourPayRate;
double grossPay;

Payroll ();

void calcPay ()

{ grossPay = numHours * hourPayRate;


int getHour (int workHour)
numHours = workHour;
return numHours;

double getGrsPay ()
{ return hourPayRate; }


int main ()
const int employees = 7;
vector <int> hours (employees);
vector <double> wage(employees);
double gp;
int count;
ifstream datafile;


if (!datafile)
cout << "Error opening data file \n";

for (count = 0; count < employees; count++)
datafile >> hours[count];
employees <hours>.getHour();/*This is my problem, how do I get it to call the getHour function with a vector?*/
datafile.close ();