Public Private and Protected Access Modifiers

Public


When creating a class the data member and the member function declared under public will be accessible by any other class or function anywhere in the code using the direct member access operator (.) with the object of that class.

Example :

[code language=”cpp”]
#include<iostream>;
using namespace std;

class square //class declaration
{
public :
double side; //class data member
double compute_area() //class member function
{
return side*side;
}
};
void main()
{
square obj; //declaring members of square as objects
obj.side=8.9;
cout<<"Side of the square is:"<<obj.side<<endl;
cout<<"Area of square is:"<<obj.compute_area();
}
[/code]

Output :

Side of the square is: 8.9
Area of the square is: 79.21

Here, side (data member of class square) and compute_area (member function of class square) where declared in the private of the class square and hence where accessible in the main function as objects .

Private


In opposition to the public modifier the private member of a class are only accessible only by the functions inside that class (or by friend function) ie. members of that class and will not be allowed to be accessed by any object or function outside the class .

Example :

[code language=”cpp”]
#include<iostream>
using namespace std;
class square //class declaration
{
private :
double side; //class data member
double compute_area() //class member function
{
return side*side;
}
};
void main()
{

square obj; //declaring members of square as objects
obj.side=8.9;
cout<<"Area of square is:"<<obj.compute_area(); //trying to access a private member
}
[/code]

Output :

In function 'void main()':
11:16: error: 'double square::side' is private
         double side;
                ^
31:9: error: within this context
     obj.side = 1.5;
         ^

Here the private members weren’t accessible by main function ,however in a case such as below

[code language=”cpp”]
#include<iostream>;
using namespace std;
class square //class declaration
{
private :
double side; //class data member
public :
double compute_area(double s)
{
side=s;
double area =side*side;
cout<<"Side of the square is:"<<side<<endl; //trying to access a private member
cout<<"Area of square is:"<<area;
}
}
};
void main()
{
square obj; //declaring members of square as objects
obj.compute_area(8.9);
}
[/code]

Output :

Side of the square is: 8.9
Area of the square is: 79.21

the private of class square side was accessed in the member function in the public of the same class.

Protected


Protected modifiers are somewhat like private modifiers with the exception that they can also be used in a class derived from the main class ie. a sub class.

Example :

[code language=”cpp”]
#include&lt;iostream&gt;;
using namespace std;
class parent //class declaration
{
protected :
double side; //class data member
};
class square :public parent //declaring a class with parent as public
{
public :
double compute_area(double s)
{
side=s;
double area =side*side;
cout<<"Side of the square is:"<<side<<endl; //trying to access a protected member
cout<<"Area of square is:"<<area;
}
};
void main()
{
square obj; //declaring members of square as objects
obj.compute_area(8.9);
}
[/code]