[solved by myself]Accessing private class members

Everywhere I read, to access private data, they say you must use a "public member function". What exactly does this mean? Do you have to do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
class whatever
{
   int first, second, third;
   public:
   float average(int, int, int);
}
whatever::average(int x, int y, int z){
x = first;
y = second;
z = third;
return (first+second+third)/3;}

or something similar?
Also, I tried this:
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
#include <iostream>
using namespace std;

class whatever
{
   float first;
   float second;
   float third;
   public:
   float average(float, float, float);
};
float whatever::average(float x, float y, float z){
x = first;
y = second;
z = third;
return (first+second+third)/3;}

int main(){
    float a, b, c;
    cout << "Enter 3 numbers: ";
    cin >> a >> b >> c;
    cin.get();
    whatever obj;
    cout << obj.average(a, b, c) << " is the solution. Press ENTER to exit...";
    cin.get();
    return 0;
}

The answer should be 5 with 4, 5, and 6 but it gives 2.(something)e+033.
Any ideas?
EDIT: needed to reverse the order of = signs in the average() function.
I get it now!!!


Last edited on
Topic archived. No new replies allowed.