How to access private members in class?

This is a discussion on How to access private members in class? within the C++ Programming forums, part of the General Programming Boards category; Hi. I received errors such as float bmi is a private members. How can I access the private members in ...

  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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    Posts
    23,012
    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
    487
    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, 09:44 AM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 10:54 AM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 07:37 AM
  5. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 08:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21