Search:     Advanced search
Browse by category:
C/C++   |   Java   |   Oracle/Database   |   SAP   |   ASP .NET   |   Mainframe-DB2   |   Freshers

What is public, protected, private?

Add comment
Views: 2243
Votes: 8
Comments: 0
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.

Others in this Category
document What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one.?
document What operations are valid on pointers? When does one get the Illegal use of pointer in function error?
document What is an Iterator class?
document What is row major and column major form of storage in matrices?
document What is the Translation Lookaside Buffer (TLB)?
document What is a nested class? Why can it be useful?
document Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()?
document Write C code to dynamically allocate one, two and three dimensional arrays (using malloc())
document How can I have a variable field width with printf?
document Maximum Sub array problem Given an array of positive and negative numbers find the sub array of maximum sum?
» More articles



RSS