some basic questions about class

A

asdf

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.
 
I

Ian Collins

asdf said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top