+ 5

What is the difference between the private and protected labels (if there's any)?

I'm just a little confused about the difference and the usage of these labels.

22nd Feb 2017, 10:49 PM
Héctor Febles
Héctor Febles - avatar
2 odpowiedzi
+ 5
private: Only members of the same class can access. protected: Is like private but derived classes can also access.
22nd Feb 2017, 11:24 PM
Isaac Salcedo
Isaac Salcedo - avatar
+ 1
oops is like your real world. try to relate it. real world: private something that you only share with your family members living in the same house. oops equivalent: private members can only be shared within class. real world: private things can also be shared with friends. oops equivalent: friend functions can access private members of the class. real world: protected things are things that you own but can be shared among your family members. oops equivalent: protected members can be shared with derived classes. real world: public things are things that can be shared with anyone. oops equivalent: public members can be shared with anyone. its easier to understand concepts if you relate them with real world examples, afterall they were developed in similar fashion. note: its just for understanding of concepts don't stick to them as rules.
23rd Feb 2017, 5:48 AM
Sizr
Sizr - avatar