+ 1

What we mean by public and private in class

9th Apr 2018, 3:03 PM
Khaled Akrimi
Khaled Akrimi - avatar
5 Réponses
+ 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
9th Apr 2018, 3:10 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 2
oh thank you Programanta ludanto
9th Apr 2018, 3:13 PM
Khaled Akrimi
Khaled Akrimi - avatar
+ 2
👍👍👍
9th Apr 2018, 3:14 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 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
9th Apr 2018, 3:38 PM
Tony Stark
Tony Stark - avatar
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
9th Apr 2018, 5:58 PM
Art I