some basic questions about class

Discussion in 'C++' started by asdf, Nov 30, 2006.

  1. asdf

    asdf Guest

    Class Aclass{
    int x;
    public:
    Aclass():x(0){};
    };

    Aclass B;

    Because B is an object of the class Aclass, and x is the private data
    member, so, B cannot access
    x directly, right? cout<<B.x would be an error.

    So, is there any way that I can change the value of x inside the object
    B?

    Thanks a lot.
     
    asdf, Nov 30, 2006
    #1
    1. Advertising

  2. asdf

    Ian Collins Guest

    asdf wrote:
    > Class Aclass{
    > int x;
    > public:
    > Aclass():x(0){};
    > };
    >
    > Aclass B;
    >
    > Because B is an object of the class Aclass, and x is the private data
    > member, so, B cannot access
    > x directly, right? cout<<B.x would be an error.
    >

    Any method of B can access whatever it wants within its self. The user
    of B can't.

    > So, is there any way that I can change the value of x inside the object
    > B?
    >

    Provide a method in Aclass to change it. That's what encapsulation is
    all about.

    --
    Ian Collins.
     
    Ian Collins, Nov 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Klett

    <div> tags, some basic questions

    Steve Klett, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    363
    Kevin Spencer
    Nov 13, 2003
  2. Michael Hesse
    Replies:
    6
    Views:
    443
    Thomas Weidenfeller
    Jul 26, 2005
  3. ccs

    Some basic questions

    ccs, Jun 11, 2004, in forum: C++
    Replies:
    6
    Views:
    347
    Mike Wahler
    Jun 11, 2004
  4. slot

    Some basic questions

    slot, Aug 3, 2004, in forum: C++
    Replies:
    8
    Views:
    366
    Default User
    Aug 3, 2004
  5. Player

    some basic questions...

    Player, Sep 20, 2004, in forum: Python
    Replies:
    3
    Views:
    368
Loading...

Share This Page