some basic questions about class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asdf

    #1

    some basic questions about class

    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.

  • Ian Collins

    #2
    Re: some basic questions about class

    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.

    Comment

    Working...