Thread: How to access private members in class?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Question How to access private members in class?

    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.

    Code:
    
    
    Code:
    #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;
    }


    Output that needs to display:
    This program will calculate your body mass index.Enter your height in meter (m) unit : 1.63Enter your weight in kilogram (kg) unit : 45Your bmi is : 16.937You are underweight.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Provide a member function that returns the bmi, then call that function from within the main function. This function should be a const member function.

    Also, I suggest that you replace the display function with another function that returns a std::string object, i.e., the status. This way, if you want to print the status, you just call the function and print the value returned. This function should be a non-member function since it just needs to call the member function that returns the bmi.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also replace these with dummy variables and call set() using those variables as arguments.
    Code:
    cout << "Enter your height in meter (m) unit : ";
        cin >> c.height;
        cout << "Enter your weight in kilogram (kg) unit : ";
        cin >> c.weight;

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by akif13 View Post
    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.
    The point of private member variables is that no code outside the class shall be able to access them. If you need to, then you're doing something wrong.
    If your case, it's about being able to set the height and weight of a BMI, which is reasonable. And this is why the BMI object should have member functions which enables you to do this since they can access private member variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    setter and getter methods

    Code:
    int getBMI()
        {
    
           return bmi = ((getWeight() / 1000) / (getHeight() * getHeight()));
    
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-20-2013, 01:25 PM
  2. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  5. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 07:06 PM

Tags for this Thread