Are accessible ONLY thru other methods of the same class where they are declared
Cannot be accessed thru a class instance
Will not be inherited
Public:
Can be data or method members
Are accessible globally from any function/method outside the class they are declared, across the application
Are accessible ONLY thru a valid instance of the class in which they are declared
Protected:
Can be data and method members
Exactly same as private members for all practical purposes, except for the inheritance part
These members are inherited by a child of the class in which they are declared
Their behaviour in the child class depends on how the inheritance has been defined
If the inheritance is private, these are private in the child class, if it is public, these are public in the child class, and if the inheritance in protected, these are protected in the child class thus allowing further inheritance.
Above answer was rated as good by the following members: ozkan, the_ankit1987
They are access modifiers and are typically used when defining a class' method(s) and variable(s). I don't think it has anything to do with security. It's just a programatic way to allow/disallow access when you are programming (in the raw code state). But if you're shipping a DLL accross private class members won't be visible.
In order of increasing restriction: public accessible to class's methods and subclasses' methods protected ? something to do with accessibility over different namespaces private access only within scope of class
public protected private are access specifiers that is used to implement encapsulation of data at various level. public - A member type public can be accessed by object protected - A member type privat can not be accessed by object. An extended class can access protected data private - Only the other members insid the class can access
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.
Are accessible ONLY thru other methods of the same class where they are declared
Cannot be accessed thru a class instance
Will not be inherited
Public:
Can be data or method members
Are accessible globally from any function/method outside the class they are declared across the application
Are accessible ONLY thru a valid instance of the class in which they are declared
Protected:
Can be data and method members
Exactly same as private members for all practical purposes except for the inheritance part
These members are inherited by a child of the class in which they are declared
Their behaviour in the child class depends on how the inheritance has been defined
If the inheritance is private these are private in the child class if it is public these are public in the child class and if the inheritance in protected these are protected in the child class thus allowing further inheritance.
In case of private inheritance base class public and protected members become private in child class. In case of protected inheritance base class public and protected members become protected in child class. In case of public inheritance base class protected members become protected in child class and public members become public in child class.
Private Protected and Public all these are access specifiers. Private Data Members are only available inside class Protected Data Members are only available inside class as well as in derived classes. Public Data members are available everywhere