class Employee
{
private:
int id; // employee ID
string name; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
Employee( int initId=0, string initName="", double initHourlyPay=0.0, int initNumDeps=0, int initType=0 ); // Constructor
bool set(int newId, string newName, double newHourlyPay, int newNumDeps, int newType);
};
And I need to get at the private members of this class using something called a 'get function.'
I understand member functions, but I'm failing to grasp how the 'get' functions would work inside of a function.
I have the get functions written into the class, but don't really get how they work or if I should just do the various calculations required required with the temp values that eventually get passed into the class.
You can call "get" functions from the main or from within other functions.
If you are working on another function within the Employee class, you don't need a "get" function. As long as you are inside that class, your function can just use the variable the normal way.
If you are outside Employee (say in the main, or in a separate class) you could do something like:
pay = employee1.get_hourlyPay();
which would fetch that employee's hourly pay and store it in a temp variable for that function to use in calculations.
If you just wanted to display an employee's hourly pay in main you could do: cout << "Employee 1's pay is $" << employee1.get_hourlyPay() << ".";
When you call a function, remember to use the object name to call it, NOT the function name. So in the above I am calling the function for "employee1" NOT "Employee" the class.
Employee::Employee( int initId, string initName, double initHourlyPay, int initNumDeps, int initType)
{
bool status = set( initId, initName, initHourlyPay, initNumDeps, initType );
if ( !status )
{
id = 0;
name = "";
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, string newName, double newHourlyPay, int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
These are the constructor and set functions.
Allow me to elaborate what I'm doing a little further: I'm reading info from a couple of files that is used to calculate various bits of an employee's paycheck, and print that to a file.
This is how I'm reading the info from the file into the main and passing it to the class.