accessing private members of a class

I have this class
1
2
3
4
5
6
7
8
9
10
11
12
13
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.'

can someone explain this to me?
A get function returns a member variable which is private or protected.

1
2
3
4
5
6
7
8
9
double get_hourlyPay()
{
    return hourlyPay;
}

int get_type()
{
    return type;
}

Thanks,

but one more question, how would I implement that into the main function?
You don't. You put them inside the class... just like the Employee() constructor and the set() function from your code snippet.

You do understand how you use member functions, don't you?
If not: http://www.cplusplus.com/doc/tutorial/classes/
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.
Hi MrBackpack.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.
1
2
3
4
5
6
file >> empId;
getline(file, empName, '#');
file >> empHourlyPay;
file >> empNumDeps;
file >> empType;
employee1.set(empId, empName, empHourlyPay, empNumDeps, empType);


should I just to the calculations with the temp variables or try it with the values in the object?

Personally, it just seems easier to do it with the temp values.
Topic archived. No new replies allowed.