Objects dealing with varibles

Im a little confused on objects concering varibles. Here is an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include iostream
class Sally
{
    public
        Sally();
        int xvar; //public just for an example
        int yvar;

int main()
{
    Sally sObj; //one object
    Sally stObj; //second object
    
    sObj.xvar = 4;
    stObj.xvar = 6;
}

};


What happens to the objects? does each object make its own COPY of xvar? or does each object change the same varible? if it makes a copy, how would i modify the varible so all objects modify the SAME var?
Last edited on
Think of it like this:

You have, on your desk, a keyboard. It's your keyboard. It's an object. Your best friend has a keyboard, too. It's not your keyboard, it's his keyboard. It's also an object. They're both objects of type 'Keyboard'

Now, your keyboard and his share some things in common. They both have keys. But his keys might be different than yours. So his variable 'mKeys' might have a different value than yours does, because his keys are different than yours are. Both keyboards have keys. Both keyboards are keyboards. They might also both have a color. Let's say yours is black. His is white. They both have the member 'mColor' but his 'mColor' is white, while yours is black.

They're the same type of object, but they're different than each other.
Topic archived. No new replies allowed.