+ 1
What we mean by public and private in class
5 odpowiedzi
+ 3
class {
public: int a; //You can see this outside the class;
private: int b; //You can't.
} temp;
temp.a; //Ok
temp.b; //Error
+ 2
oh thank you Programanta ludanto
+ 2
👍👍👍
+ 1
private cant be accessed by object as it is like you own property which is tried to be accessed by other but cant do it.
and public can be accessed by object outside the class as it is like a park which is public and can be accessed by anyone
0
It's all about object oriented programming (OOP).
One of its principled is to hide variables from outer user. Any value you want to implement in your class should be accessed with proper methods:
class A {
int val;
public:
A(){ val =0; }
int getVal() { return val;}
void setVal( int v ){
if(v>4) v=4;
if(v<-4) v=-4;
val=v;
}
};
The setter grants val to be from -4 to 4.
So, folowing the principle, internal values of you class will always be correct