Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

A very basic question about objects of a class

Hello gurus,

I would like to ask a very basic question about C++ class. I have been reading teach yourself c++ in 21 days. One of the Q&As of day6(basic classes) is as the following:

**********************************************************
Q: If I declare a class Cat with a private member itsAge and then define two Cat objects, Frisky and Boots, can Boots access Frisky's itsAge member variable?

A: Yes. Private data is available to the member functions of a class,and different instances of the class can access each other's data. In other words, if Frisky and Boots are both instances of Cat, Frisky's member functuions can access Frisky's data and also Boots's data.

**********************************************************

I understand the first half of the explanation which is "Private data is available to the member functions of a class". I am confused about the second part which is " different instances of the class can access each other's data".

If I create two objects(Frisky and Boots) on stack, they have two different memory addresses for their own variable. It is itsAge in this case. It's true that Frisky's member functuions can access Frisky's data. But why Frisky's member functuions can also access Boots's data?

I thought the only way to access Boots's data is to use something like Boots.GetAge().

The simple test program is as following:

**********************************************************

#include <iostream>
using namespace std;

class Cat
{

public:
Cat();
~Cat();
void SetAge(int age) { itsAge=age;}
int GetAge() { return itsAge;}

private:

int itsAge;

};

Cat::Cat()
{
cout<<"Constructor called.\n";
itsAge=1;

}

Cat::~Cat()
{

cout<<"Destructor called.\n";


}

int main()
{
Cat Frisky;
Cat Boots;

Frisky.SetAge(10);
Boots.SetAge(8);

cout<<Frisky.GetAge()<<endl;
cout<<Boots.GetAge()<<endl;


return 0;

}

**********************************************************

I just don't know how to write to make Frisky access Boots's age. Could you please add some lines in my test program to do so?

Thank you very much for your time!

Sincerely,
yahoo
[2564 byte] By [yahoo] at [2007-11-11 10:02:55]
# 1 Re: A very basic question about objects of a class
Is it the exact question ? I think he mean Boots and Frisky are members from the same class , or the only method I know is to access static members !

It's strongly similar to this :

int a;
int b;
a = 5;
b = 4;
//No [at all] any relation between a and b !
Only if they have comman variable , which is not cleared in the question or e ment a "member" coz the answer tell us about this : "Private data is available to the member functions of a class" .

MoreOver u can make a specified function doin that like this :
void SetOAge(Cat& age) { age.itsAge=itsAge;}
calling it using that:
Frisky.SetOAge(Boots);
Amahdy at 2007-11-11 20:59:29 >
# 2 Re: A very basic question about objects of a class
I think this is what he meant :

#include <iostream>
using namespace std;

class Cat
{
public:
int test2;
void setTest1(int a) {test1=a;}

private:
int test1;
};

int main()
{
Cat Frisky;
Cat Boots;

Boots.test2 = 5;
Boots.setTest1(10);

Frisky.test2 = Boots.test1; //Error can't access private
Frisky.test2 = Boots.test2; //Ok I can access public
Frisky.setTest1(Boots.test2); //Ok I can here too.

return 0;

}
Amahdy at 2007-11-11 21:00:34 >
# 3 Re: A very basic question about objects of a class
Yes, this is the exact question and answer. I copied it from the book.

By defining a public variable test2, Frisky doesn't seem to access Boots' private variable test1:)

Do you think this makes any sense:

********************************************************

int main()
{
Cat Frisky;
Cat Boots;

Frisky.SetAge(10);
Boots.SetAge(8);

cout<<Frisky.GetAge()<<endl;
cout<<Boots.GetAge()<<endl;

Frisky.SetAge( Boots.GetAge());
cout<<Frisky.GetAge()<<endl;

return 0;

}

**********************************************************

Frisky's GetAge() method is accessing Boots' private variable itsAge, at least kind of? Maybe this is what the question meant.
yahoo at 2007-11-11 21:01:33 >
# 4 Re: A very basic question about objects of a class
Your book got it right. To understand this explanation better, let's clarify it a bit. Memebr functions are defined for a class, not per object. In other words, you don't define individual member functions for every object. Private data members by definition are declared in the class too, but each object gets its own instances of those data members, on a separate memory location as you pointed out. The private acccess specifier (as all access specifiers) applies to classes, not obejcts. Therefore, a private data member can't be accessed from an object of a different class. However, two objects of the same type can access each other's private data members directly.
This property often surprises C++ programmers who mistakenly assume that an object can only access its own data members. Why is it so? You have to rerember that private isn't a security measure. Rather, it's a design tool meant to ensure that implementation details of a certain class aren't exposed to other classes. It doesn't make any sense to hide implementation details (i.e., private data members) from an object A that already knows the precise implementation details of an object B, since they both have the same type.
If you want to create object-local data, you'd normally use local variables inside member functions, not data members.
Danny at 2007-11-11 21:02:39 >
# 5 Re: A very basic question about objects of a class
Yes Danny, that's what was listed here :

void SetOAge(Cat& age) { age.itsAge=itsAge;}

as "itsAge" is private but acceed by the function member of the class .
The essential now u can good understand the structure of the class so u will not get confusion , as u implement in your last post.
Amahdy at 2007-11-11 21:03:32 >
# 6 Re: A very basic question about objects of a class
Thank you very much for your reply, Danny. I appreciate it.

Your explanation makes perfect sense and it deepens my understanding of the class objects. I thought an object can only access its own data members before too.

Here leads to the one more thing. Did you mean, in my simple test code, Frisky can literally access Boots' memory? If so, could you please add few lines to my simple code? I just couldn't write to make that happen. I still think my last post and Amahdy's last post were not really showing one object accessing another's private variable. We were just passing the private values between two objects. Not really "accessing".

The bottom line is could you please add few lines to show me how? I got stuck there for too long :WAVE:
yahoo at 2007-11-11 21:04:38 >
# 7 Re: A very basic question about objects of a class
Sure, here's a simpler example that shows this phonemenon:
class X
{
private:
int y;
public:
void set(X& r)
{
r.y=0; //change another object's private data
}
};

int main()
{
X a, b;
a.set(b);
}
Not that I would recommend writing such code... but you can see the point: set doesn't know whether r is another X object. Most likely, it is, but r could alse be the same as a:
a.set(a) //modify a.y
Danny at 2007-11-11 21:05:42 >
# 8 Re: A very basic question about objects of a class
Danny, it makes perfect sense again. Now I got it. Thank you very much for bearing with a yahoo like me :)

Amahdy, you were right all along. sorry about not understanding you at first :p
yahoo at 2007-11-11 21:06:39 >