A very basic question about objects of a class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: A very basic question about objects of a class

  1. #1
    Join Date
    Dec 2006
    Posts
    4

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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);
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    I think this is what he meant :

    Code:
    #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;
    
    }
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Dec 2006
    Posts
    4

    Thanks for the reply,Amahdy

    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.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  6. #6
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Yes Danny, that's what was listed here :

    Quote Originally Posted by Amahdy
    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.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  7. #7
    Join Date
    Dec 2006
    Posts
    4

    Thanks a lot, Danny! one more thing:)

    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:

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    Sure, here's a simpler example that shows this phonemenon:
    Code:
    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
    Last edited by Danny; 12-19-2006 at 05:38 PM.
    Danny Kalev

  9. #9
    Join Date
    Dec 2006
    Posts
    4

    Danny, Amahdy, thank you guys so much!

    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

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. Replies: 2
    Last Post: 02-24-2006, 11:01 PM
  3. Java class IMPORTANT question
    By blarblar in forum Java
    Replies: 1
    Last Post: 10-04-2005, 09:49 AM
  4. Calling base class methods
    By GR in forum Java
    Replies: 1
    Last Post: 11-09-2000, 05:38 PM
  5. Simple Class Variables question
    By Gary McCallum in forum Java
    Replies: 2
    Last Post: 10-20-2000, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center