Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: [C++] Getting one object to access another object's data members

  1. May 12, 2010 #1
    1. The problem statement, all variables and given/known data
    see #3


    2. Relevant equations
    N/A



    3. The attempt at a solution
    I'm working on this assignment and I got stuck at this part in programming here's what I need to do:

    I need to find a way to have one object access the data members of another object.

    A broken down/simplified version of my code looks like this:
    Code (Text):

    class ClassOne
    {
    public:
        ClassOne();
        int m_SomeData;
        string getData();
    };

    ClassOne::ClassOne()  //setting data to 1
    {
        m_SomeData = 1;
    }

    string ClassOne::getData()
    {
        return m_SomeData;
    }
     
    What I want to do is:
    Code (Text):

    class ClassTwo
    {
    public:
        void showData();
    };


    void ClassTwo::showData()
    {
        //pseudocode: cout << m_SomeData from ClassOne;
        //how do I get this line to work?
    }
     
    I know I have to pass in parameters to showData() and possibly dereference but I don't know what to pass in.
     
  2. jcsd
  3. May 12, 2010 #2

    Mark44

    Staff: Mentor

    ClassOne is public, and so are its data members. If you create a ClassOne object, any other class has full access to the data and methods on ClassOne. To do what you want to do, I would write showData so that it has a parameter that is a reference to a ClassOne object.

    In practice, though what you are trying to do is very seldom done, and is to be avoided. The data members of a class are usually declared using the private or protected access specifier, not public. Making the data private promotes encapsulation via data hiding.
     
  4. May 12, 2010 #3
    Can you show me how that code looks? I know I sound like I'm asking to be spoon-fed but the text book I have does not have those examples.
     
  5. May 12, 2010 #4

    Mark44

    Staff: Mentor

    Code (Text):
    void classTwo::showData(ClassOne & classOneobj)
    {
       // code for method
    }
     
    Inside this method you can call getData or evaluate m_someData directly.
     
  6. May 12, 2010 #5
    Code (Text):
    void classTwo::showData(ClassOne & classOneobj)
    {
       cout << m_SomeData;
    }
    My compiler wants me to declare m_SomeData in classTwo because it's giving me an undeclared identifier error. I thought passing in parameters (ClassOne & classOneobj) was suppose to eliminate the need to do that...?
     
  7. May 12, 2010 #6

    Mark44

    Staff: Mentor

    m_SomeData is out of scope, so you can't use it that way. The idea of passing in a parameter to a function is to actually use the parameter, which you haven't done.

    If you have an instance of a class, how do you use a method or property on that object?
     
  8. May 13, 2010 #7
    Oh, duh.

    Code (Text):
    void ClassTwo::showData(ClassOne & classOneobj)
    {
       classOneobj.getData();
    }
    Thank you.

    One last question:
    Code (Text):
    int main() {
        ClassTwo dummy;
        dummy.showData();
        return 0;
    }
    dummy.showData(); wants parameters passed into it. What would they be?

    Parameters with data types like strings and ints, I know to pass in strings and ints

    void someFunction (string someString)
    someObject.someFunction("this is a string");

    But what would the parameters for a reference be?

    Edit: I tried doing: dummy.showData(ClassOne & dummy); but coming back with an "illegal use of expression" error.

    Edit 2: Okay, I got it. It should be

    Code (Text):
    int main() {
        ClassTwo dummy;
        ClassOne dummy1;
        dummy.showData(dummy1);
        return 0;
    }
    Thank you for everything Mark.
     
    Last edited: May 13, 2010
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook