Problem with Private Variables in Class

I'm having trouble with private variables in a class.
I was able to solve my problem by making those variables public, however this is not what is desired. Can anyone provide some assistance?
The basic problem is with the "total" variable.
I can't use it in the function down at the very bottom sortReimbursements, but I can use it if I put the variables in the public part of 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class Employee
{
    private:
    int id, numMeals;
    double miles, lodging, meals, total;

    public:
    // double total;
    // It works fine if total is here.

    Employee()
    {cout << "Object Created!\n"; }
    void readData(ifstream &infile)
    {
        double nextMeal = 0;
        infile >> id;
        infile >> miles;
        infile >> lodging;
        infile >> numMeals;
	infile >> meals; //read in first meal
        for(int i = 2; i <= numMeals; i++)
        { //while there's still meals, read more
            infile >> nextMeal;
            meals = meals + nextMeal;
        } // and accumulate it in the total meals
    }
    void calcReimbursement()
    {
        total = lodging + (miles * 0.43) + meals;
    }
    void printResults(ofstream &outfile)
    {
        outfile << id
                << setprecision(2) << fixed
                << setw(15) << miles
                << setw(15) << lodging
                << setw(15) << meals
                << setw(15) << total
                << endl;
    }
    ~Employee()
    {cout << "Object Destroyed!\n";}
};

void printHeadings(ofstream &);
void sortReimbursements(Employee *, Employee &);
#define SIZE 7
int main()
{
     Employee e; //temporary object holder for swap
     Employee ray[SIZE]; //Declaring an array of objects

    ifstream infile; infile.open("input.txt"); //open infile
    ofstream outfile; outfile.open("output.out"); //open outfile

    printHeadings(outfile); // Print Headings to outfile

    ray[0].readData(infile); //read first set of data
    while (!infile.eof()) //while not at end of file
        for(int i = 1; i <= SIZE-1; i++)
		ray[i].readData(infile); //read rest of data

	for(int i = 0; i <= SIZE-1; i++)
		ray[i].calcReimbursement(); //Calculate reimbursements

	sortReimbursements(ray, e); //Sorts the reimbursements

	for(int i = 0; i <= SIZE-1; i++)
		ray[i].printResults(outfile);

    infile.close(); outfile.close();
    return 0;
}
void printHeadings(ofstream &outfile)
{
    outfile << setw(45) << "James D'Aniello\n"
            << setw(44) << "8 April 2010\n\n"
            << setw(51) << "Employee Expense Report\n\n"
            << setw(5) << "ID"
            << setw(18) << "Miles"
            << setw(16) << "Lodging"
            << setw(14) << "Meals"
            << setw(21) << "Reimbursement\n\n";
}
void sortReimbursements(Employee *ray, Employee &e)
{ //The pointer "*" brings in the array
	for(int j = 1; j <= SIZE-1; j++)//bubble sort
	{
		for(int i = 0; i < SIZE-j; i++)
		{
			if(ray[i].total < ray[i+1].total)//swap
			{
				e = ray[i];
				ray[i] = ray[i+1];
				ray[i+1] = e;
			}
		}
	}
}
If you want to able to access them, they must be public or have getter/setter methods.
Thanks, I forgot that I had to 'get' it from the class. I'll post again if I have any problems.
How exactly do I 'get' it? My teacher went over that part really fast at the end of class and I'm just lost. I tried google, but I couldn't find exactly what I was looking for, I was probably searching wrong, I'm sorry to bother.
Getters generally look like this:

variable_type ClassName::get_variable_name() { return this->variable_name; }
Never mind. I solved it. It's a rough workaround but it works. There's probably an easier way, but oh well.
I added an array in main.
double totals[SIZE]
And then I grabbed the value in total from the class and put it into that array.
and then the last function changed to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sortReimbursements(Employee *ray, Employee &e, double *totals)
{ //The pointer "*" brings in the array
	for(int j = 1; j <= SIZE-1; j++)//bubble sort
	{
		for(int i = 0; i < SIZE-j; i++)
		{
			if(total[i] < total[i+1])//swap
			{
				e = ray[i];
				ray[i] = ray[i+1];
				ray[i+1] = e;
			}
		}
	}
}
Oh, I didn't get your reply till after I posted my solution.
But, thanks for all your help.
I'll try to implement the getter but at least I have a fall back now if that doesn't work.
Thanks again.
Hm, actually, I think total shouldn't even be a variable, it should be a member function that just adds up the other variables inside the class.
Total is actually the reimbursement.
I should've named it reimbursement instead, I just kinda stuck with total.
The function that you're referring to would be calcReimbursement.
Ah, I see. I'm not sure why you are storing the result in basically a temporary variable. Why don't you just have calcReimbursement return the value it got?
That is a very good question. It's just habit. My teacher told us to do that in the beginning and then she said we could just do it in the return statement and forget the temporary variable, but it stuck with me. I actually don't use temporary variables that much now, this is actually redoing an older program to work with an array of objects.
Topic archived. No new replies allowed.