What is the difference between the private and protected labels (if there's any)? | Sololearn: Learn to code for FREE!

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

c++

2/22/2017 10:49:06 PM

Héctor Febles

2 Answers

New Answer

+5

private: Only members of the same class can access. protected: Is like private but derived classes can also access.

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