What is the scope of private, public and protected in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the scope of private, public and protected in c++?

2nd Dec 2019, 12:47 PM
Swathy Kumaravelu
Swathy Kumaravelu - avatar
3 Answers
+ 5
Private specifier is used when you want to hide data completely from other classes, When a variable is declared private it can not be accessed from anywhere except same class functions. Protected is used when you are using inheritance and want to share variables, Protected variable can be used from same class as well as inherited child class of that class. Public means using it anywhere with object. It can be accessed from any class or any method
2nd Dec 2019, 1:12 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Private only to the class where it is defined Protected to the class and its child class Public every where
3rd Dec 2019, 7:52 AM
Rohit