How to access private members
Hi. I received errors such as float bmi is a private members. How can I access the private members in the main? Thanks in advance
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
|
#include <iostream>
using namespace std;
class BMI
{
private:
float height, weight, bmi;
public:
BMI()
{
height = 1.0;
weight = 1.0;
}
void set(float a, float b)
{
height = a;
weight = b;
}
void calculate()
{
bmi = ((weight / 1000) / (height * height));
}
void display()
{
string status;
if (bmi < 18.5)
{
status = "Underweight";
}
else if (bmi > 18.5 && bmi < 24.9)
{
status = "Normal";
}
else if (bmi > 25 && bmi < 29.9)
{
status = "Overweight";
}
else
{
status = "Obese";
}
cout << status;
}
};
int main()
{
BMI c;
cout << "This program will calculate your body mass index." << endl;
cout << "Enter your height in meter (m) unit : ";
cin >> c.height;
cout << "Enter your weight in kilogram (kg) unit : ";
cin >> c.weight;
cout << "Your bmi is : " << c.bmi << endl;
c.display();
return 0;
}
|
How can I access the private members in the main? |
Don't. An you don't need to. Collect the height and weight in local variables, then call BMI::set () to update the BMI object.
Also, a person with BMI of 18.5 is not obese.
Topic archived. No new replies allowed.