-2

make a program output

// the values of:
//
//   * `thing->x`
//   * `thing->y`
//   * `thing->foo()`
//   * `thing->bar()`

 using namespace std;
class Thing
{
private:
    int x;
    int y;
    virtual int foo()
    {
        return x + y;
    }
    virtual int bar()
    {
        return x*y;
    }
public:
    Thing() {
        x = 2;
        y = 10;
    }
};

int extract_x(void *thing)
{

    Thing thing;
    Thing();

    cout << x;

    return 0;


}

int extract_y(void *thing)
{
    Thing thing;
    Thing();

    cout << y;

    return 0;
    // --- End your code   ---
}

int call_foo(void *thing)
{




    // --- Begin your code ---
    return 0;
    // --- End your code   ---
}

int call_bar(void* thing)
{
    // --- Begin your code ---
    return 0;
    // --- End your code   ---
}


int main()
{
    Thing thing;
    std::printf("%d %d %d %d\n",
        extract_x(&thing),
        extract_y(&thing),
        call_foo(&thing),
        call_bar(&thing));
    system("pause");
    return 0;
}

Here I am trying to create new object and the grab the variables in the Thing class but it still does not work. Says that x is undefined. I am not supposed to touch anything but the bodies of extraxt_x,extract_y,call_foo, and call_bar

J Knight
  • 1
  • 2
  • 1
    "not working" is not a proper description of a problem. What did you try? Did you get a compiler error? What did it say? Have you tried researching it? (Perhaps that might lead to an answer) – TheUndeadFish Mar 21 '17 at 02:32
  • 2
    Possible duplicate of [Private and Protected Members : C++](http://stackoverflow.com/questions/224966/private-and-protected-members-c) – Weak to Enuma Elish Mar 21 '17 at 02:32
  • 1
    It looks like you've replicated an assignment where everything was handed to you on a silver platter. Just do exactly what the assignment tells you and you won't have a problem. – Disillusioned Mar 21 '17 at 02:35
  • What have ***you*** tried? That code you've provided doesn't look like something you tried given the nature of your question. It looks very much like the stub code from an assignment - implying you haven't tried anything (even though the answer is right in front of you). – Disillusioned Mar 21 '17 at 02:43
  • Im trying to practice pointers I thought in this assignment we could just return thing>x and return thing>y . When I try this it says x is undefined and y also . In the problem the x and y are public why cant I just grab those, or do I have to do Thing thing=new Thing in order to access these. Sorry if this was wrong spot for learning first time posting here. – J Knight Mar 21 '17 at 03:07
  • Never mind on that part that is what we do in main. I will keep fooling around until I get something. – J Knight Mar 21 '17 at 03:15
  • @JKnight What makes you think `x` and `y` are public? And I'm now noticing why you might be having trouble. – Disillusioned Mar 21 '17 at 03:40
  • Guess I thought that when I passed *thing into each function this gave me access to the x=2, x=y in public Thing(){} – J Knight Mar 21 '17 at 03:44
  • But I guess I see they are completely different variables. One in Thing() and the other in my Class Thing() – J Knight Mar 21 '17 at 03:47
  • @JKnight "Sorry if this was wrong spot for learning first time posting here" This is a great place to post for help _provided you make the effort to ask a good question_. As other's mentioned, you saying "_it's not working_" (without providing the error message) and not showing what **you actually did** makes it impossible for someone else to _help you understand what you did wrong_ ... because we have insufficient information to deduce what you did wrong. – Disillusioned Mar 21 '17 at 04:23
  • They're not "different variables". There is only 1 `x` variable and 1 `y` variable. The constructor `Thing()` can access the **private** variables because it's a member of the `Thing` class. The `extract_x` function is not a member of `Thing`, so it cannot access the `x` member of the `Thing` class because `x` is **private**. – Disillusioned Mar 21 '17 at 04:28
  • Okay so then I did have the right understanding but I did word it wrong when I said int x and int y were public. I said it meaning that Thing() could access them. So I should be able to just create new thing in extract function and just return x ? – J Knight Mar 21 '17 at 05:31
  • Well tried that that does not work.I get x not defined So I am going to make and int x in extract_x and have to find a way to point that to the variable x in constructor Thing(). – J Knight Mar 21 '17 at 06:06

1 Answers1

0

C++ rarely requires to use pointers with a proper OO design. In the example you gave you could use references instead of pointers. In addition if you manipulate an object use the proper class for the argument instead of void * eg

int extract_x(const Thing &thing)
{
   // --- Begin your code ---
   return 0;
   // --- End your code   ---
}

You can also define getX() and getY() public method and set foo and bar as public

int Thing::getX(void) const
{
   return x;
}
pascal sautot
  • 375
  • 6
  • 20
  • Okay I will look up referencing I have seen some on that, the problem says that I should be able to do everything I need with only putting code in the area listed, so I must leave the functions the way they are. Thanks for giving me reference idea. Not looking to get problem done for me but I learn visually so with no example is hard to understand. Ill let you guys know what I get from using reference. – J Knight Mar 21 '17 at 12:33