| Home / C & C ++ / What is public, protected, private? |
What is public, protected, private? |
||||
public, protected, private are access specifiers that is used to implement encapsulation of data at various level.
Private: * Can be data or method members * Are private to the class where they are declared * Accessible ONLY by the member methods of the class where they are declared * Only exception to the above rule is Friend (explanation of friends is beyond the scope of this topic * In a C++ class, private is default for member declaration. That is, if you do not specify any access specifier (private, public, protected), the member is considered private Public: * Can be data or method members * Are accessible by any function/method/application globally, so long as an instance of the class where the public members are declared is created. * These members are accessible only thru an instance of the class where they are declared * Generally used to define a C++ class behaviour and/or to access private data members (act as private data modifiers) Protected * Can be data or method members * Act exactly as private members for all practical purposes, so long as they are referenced from within the class (and/or instances of the class)where they are declared * Specifically used to define how certain data/method members of a class would behave in a child class (used to define their behaviour in inheritance) * The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance. |
||||
Powered by
KBPublisher (Knowledge base software)
