private public and protected
What is Private, public and protected? Or explain access specifiers.
Public: Public members can be access through any object of that class. We can access public data anywhere in the program means in the class also and main also.
Private: We can access private data within that class only not outside of that class means not in the main. All data and methods of class are private by default.
Protected: v A member declared as protected is accessible by the member functions within its class and by the any class immediately derived from it.
Difference between private and public access specifier.
Protected: v A member declared as protected is accessible by the member functions within its class and by the any class immediately derived from it.
Difference between private and public access specifier.
Private
|
Public
|
All data and methods of class are private by default.
|
All members of a class are not public by default.
|
Private members cannot be access through any object of that class.
|
Public members can be access through any object of that class.
|
We cannot access private data anywhere in the program
|
We can access public data anywhere in the program
|
Example class student
{
private:
int a;
}
|
Example class student
{
public:
int a;
}
|