0

I know that private class data is only accessible within the class; however, the examples that I have read show similar use of private members in their program code. I am attempting to use functions to access and manipulate the private class members, but it is not working. What am I doing incorrectly? I have tried substituting the Data.SelectionF() function for Data.selection after the first cin and for all instances of the use of the selection member variables without success. I also tried the same approach for all instances of the value member variable. Thanks

 #include <iostream>
    #include <iomanip>

    using namespace std;    

    class allData {

    private: 
        char selection; 
        double r; 
        double centimeter; 
        double value; 
    public: 
        double ConvertC (double value);
        double ConvertR (double value);
        double valueF (double value); 
        char selectionF (char selection); 
        allData Data (); 

    } Data;

    int main() {

    cout << "Enter C for converting your Feet to Centimeters.\n"
            "Enter R for converting your Inches to Centimeters.\n";

    cin >> Data.selection; 

    cout << "\nYou selected to convert to: " <<   
             Data.selectionF(Data.selection) << ".\n\n"; 

    cout << "Enter your starting value to two decimal places, and press 
             ENTER.\n\n"; 

    cin >> Data.value; 

    cout << "\nYou entered a starting value of: " << 
              Data.valueF(Data.value) << ".\n\n"; 

    //switch to decide which conversion function to use from the structure

    switch (Data.selectionF(Data.selection)) {

        case 'c': { Data.ConvertC(Data.value);  
            cout << "Your Feet converted to Centimeters is: " << 
            Data.ConvertC(Data.value) << "\n\n"; 
            break; 
                  }

        case 'C': { Data.ConvertC(Data.value);  
             cout << "Your Feet converted to Centimeters is: " << 
             Data.ConvertC(Data.value) << "\n\n"; 
             break; 
                  }
        case 'r': { Data.ConvertR(Data.value);  
             cout << "Your Inches converted to Centimeters is: " << 
             Data.ConvertR(Data.value) << "\n\n"; 
             break; 
                  }

        case 'R': { Data.ConvertR(Data.value);  
            cout << "Your Inches converted to Centimeters is: " << 
            Data.ConvertR(Data.value) << "\n\n"; 
            break; 
                  }
       default: {cout << "You entered an invalid selection for your conversion"   
            "choice.\n"; 
            break; 
                 }
        }

    return 0; 

       }

    //Function definitions
    double allData::ConvertC (double value) {
        centimeter = value * 30.48;
        return centimeter; 
    }

    double allData::ConvertR (double value) {
        r = value * 2.54; 
        return r; 
    }
    double allData::valueF (double value) {
        return value; 
    }

    char allData::selectionF (char selection) {
        return selection; 
    }

    //End of program.
  • I think you must be misunderstanding the examples. Read up on "getters and setters in C++" and then try this again. The pattern you need is `cin >> localVar; classInstance.setProperty(localVar);` – Steve Townsend Jul 02 '16 at 20:29
  • [using namespace std is a bad idea](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – Ed Heal Jul 02 '16 at 20:33

2 Answers2

2

Whatever examples you've read, either they are wrong or you misunderstood them. You cannot access private class members outside of the class (barring friend declarations, of course). That's what a private class member means, by definition.

cin >> Data.selection; 

selection is a private class member. It cannot be accessed from your main(). That's, pretty much, all that can be said about it.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I've never tried but I wonder if this would be valid syntax to declare main in the same namespace: ` int allData::main() {` – ifma Jul 02 '16 at 20:40
  • 1
    The syntax would be valid. However it will not be the main() function that gets executed when the application starts. It will be just another class method, no different than allData::foo(), or allData::bar(). – Sam Varshavchik Jul 02 '16 at 20:53
0

If I did not misunderstand your question let me tell you something about your problem. Firstly, with this line "cin >> Data.value;" you are trying to get the private value of the "DATA" class in fact this is wrong. You cannot access to the private variable from the outside of the "DATA" class. The object "DATA" is not inside the "DATA" class. On the other hand if you want to access the private variables inside the class indirectly, then you can write a function like that:

public:
void function_name(const DATA &dataObject) const
{
    cout << "The selection value : " << dataObject.selection << endl;
}

This part of code will access to the private variables inside the class indirectly. Remember if a function is inside the class then it can access all the private variables and there is no something wrong with this code.

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • So...within the context of the variables that i provided, how would I write this? I tried something similar to what was mentioned without success. – cppstudent1 Jul 03 '16 at 03:31