+ 1

What's the difference between class private and public?

21st Dec 2016, 10:06 PM
Sri Andriyani
Sri Andriyani - avatar
3 Answers
+ 9
Ok! So... There are three different classes; Public, private and protected. Down below is a script I scratched down to easier display. Classes B, C and D all contain the variables x, y and z. It is just a question of access. Simply about usage of protected and private access.  The script: class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D }; }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D }; }; class D : private A { // 'private' is default for classes { // x is private // y is private // z is not accessible from D }; Dr.
21st Dec 2016, 10:23 PM
Tristan McCullen
Tristan McCullen - avatar
+ 6
A public method or field can be accessed from any object. It's used for entry points into classes, getters and setters, and methods with use outside the class. A private method or field can only be used within its class. It's used when a method or field doesn't need to be used or modified outside the class.
21st Dec 2016, 10:23 PM
Tamra
Tamra - avatar
+ 2
Good question. I will answer this shortly, stay tuned. :)
21st Dec 2016, 10:13 PM
Tristan McCullen
Tristan McCullen - avatar