access class member variables

Feb 11, 2013 at 8:26pm
I need to access variables from a class member function, outside of the class. I'm not sure how to do this? I attempted with bits and pieces I've seen, but it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class BMI {
public:
void getHeightWeight() {
    weight = 10;
    height = 10;
}

void calcBMI(int weight, int height) {
     etc..etc..
}
};

int main() {
     BMI BMI;
     BMI.getHeightWeight;
     BMI.calcBMI(BMI.getHeightWeight.weight,BMI.getHeightWeight.height);
}
Last edited on Feb 11, 2013 at 8:29pm
Feb 11, 2013 at 8:38pm
1
2
3
4
5
6
7
8
9
10
11
12
13
class BMI
{
    int w, h;
public:
    BMI(int weight, height) : w(weight), h(height)
    {
    }

    int Calculate()
    {
        return /*stuff with w and h*/;
    }
};
1
2
BMI bmi (10, 10);
std::cout << bmi.Calculate() << std::endl;
Feb 11, 2013 at 8:44pm
This is a constructor isn't it?

1
2
3
4
public:
    BMI(int weight, height) : w(weight), h(height)
    {
    }


This won't work because I want the member function getHeightWeight to accept the user input (looks better then throwing it all in main?)

Here is the actual code:

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

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

class BMI {
public:

void calcBMI(float weight, float height) {
		int selectBMI = 0;
        float BMI = 0;
        BMI = (weight * 703) / (height * height);
        string weightClass;
		if (BMI < 18.5)
			selectBMI = 1;
		else if (BMI >= 18.5 && BMI <= 25.0)
			selectBMI = 2;
		else if (BMI > 25.0)
			selectBMI = 3;
		else
			selectBMI = 0;
        switch (selectBMI) {
            case 1 :
                weightClass = "You Have Optimal Weight.";
				break;
			case 2 :
                weightClass = "You Are Underweight.";
				break;
			case 3 :
                weightClass = "You Are Overweight.";
				break;
			default :
                weightClass = "Please enter a weight integer.";
				break;
		}
        cout << "Your Body Mass Index (BMI) is: " << BMI << endl;
		cout << weightClass;
}

void getHeightWeight() {
            float weight = 0;
            float height = 0;
            cout << "Please enter your weight in lbs: ";
            cin >> weight;
            cout << "Please enter your height in inches: ";
            cin >> height;
            cout << endl;
			cout << endl;
            cout << endl;
			cout << endl;
}
};


int main() {
	cout << "Program to determine your health." << endl;
	cout << endl;
	cout << endl;
	BMI BMI;
	BMI.getHeightWeight();
	BMI.calcBMI(BMI.weight,BMI.getHeightWeight.height);
}


How would I do it this way?
Last edited on Feb 11, 2013 at 8:45pm
Feb 11, 2013 at 8:46pm
The point of class design is that the class should be independent and should not care how it is used. By having the input handled by the class, it is not independent and you are circumventing class design. This is a scenario where class design is not useful.
Feb 11, 2013 at 8:58pm
ok L B - I rerouted the input function to the main function and only used the BMI class to hold function to calculate BMI, after the user input. I'm going to rename the class 'health', and have it hold several member functions to do different calculations for health related concerns. Is this a appropriate use of classes?

Also - How is 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
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
class BMI {
public:

void calcBMI(float weight, float height) {
		int selectBMI = 0;
        float BMI = 0;
        BMI = (weight * 703) / (height * height);
        string weightClass;
		if (BMI < 18.5)
			selectBMI = 1;
		else if (BMI >= 18.5 && BMI <= 25.0)
			selectBMI = 2;
		else if (BMI > 25.0)
			selectBMI = 3;
		else
			selectBMI = 0;
        switch (selectBMI) {
            case 1 :
                weightClass = "You Have Optimal Weight.";
				break;
			case 2 :
                weightClass = "You Are Underweight.";
				break;
			case 3 :
                weightClass = "You Are Overweight.";
				break;
			default :
                weightClass = "Please enter a weight integer.";
				break;
		}
        cout << "Your Body Mass Index (BMI) is: " << BMI << endl;
		cout << weightClass;
		cout << endl;
}
};

int main() {
	BMI bmi;
	float weight = 0;
        float height = 0;
	cout << "Program to determine your health." << endl;
	cout << endl;
	cout << endl;
        cout << "Please enter your weight in lbs: ";
        cin >> weight;
        cout << "Please enter your height in inches: ";
        cin >> height;
        cout << endl;
	cout << endl;
        cout << endl;
	cout << endl;
	bmi.calcBMI(weight, height);
}
Last edited on Feb 11, 2013 at 8:59pm
Feb 11, 2013 at 9:00pm
Your class does not have a state and therefore should not be a class.

The correct way to group similar stateless functions is to use a namespace:
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace Health
{
    int CalculateBMI(int weight, int height)
    {
        return /**/;
    }

    //other functions...

    //other functions...

   //...
}
Feb 11, 2013 at 9:01pm
roger that. Thanks@! Back to the books...
Last edited on Feb 11, 2013 at 9:02pm
Feb 11, 2013 at 9:02pm
Forgot to mention, you would need to type

Health::CalculateBMI(blah, blah);

to call it from outside the namespace, just like with the std namepsace.
Last edited on Feb 11, 2013 at 9:02pm
Topic archived. No new replies allowed.