Feb 11, 2013 at 8:26pm UTC
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 UTC
Feb 11, 2013 at 8:44pm UTC
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 UTC
Feb 11, 2013 at 8:46pm UTC
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 UTC
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 UTC
Feb 11, 2013 at 9:01pm UTC
roger that. Thanks@! Back to the books...
Last edited on Feb 11, 2013 at 9:02pm UTC
Feb 11, 2013 at 9:02pm UTC
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 UTC