The main aim of Object oriented programming is to hide the data and provide security to the application users.It can be achieved by using modifiers of classes. Modifiers restricts the hackers without providing the access to the class members and data present in it. So, modifiers also called as "Access specifiers". There are mainly three access specifiers.
Access Specifiers
public.
private.
protected.
public
Description
The member of a class with public access specifier is accessed anywhere outside the class but only inside the program. The variables declared with public keyword can be retrieved and accessed without member function.
Example
[c]
#include <iostream>
using namespace std;
class Triangle
{
public:
double base;
void setBase( double bas );
double getBase( void );
};
// Member functions definitions
double Triangle::getBase(void)
{
return base ;
}
void Triangle::setBase( double bas )
{
base = bas;
}
// Main function for the program
int main( )
{
Triangle tri;
// set line base
tri.setBase(3.0);
cout << "Base of triangle : " << tri.getBase() <<endl;
// set line length without member function
tri.base = 6.0; // base is public
cout << "Base of triangle is : " << tri.base <<endl;
return 0;
}[/c]
Output
[c]
Base of triangle : 3
Base of triangle is : 6
[/c]
private
Description
The member of a class with private access specifier is not accessible and viewed outside of the class. The variables declared with private keyword can be retrieved and accessed only by class and neighbor functions.All the members of class are declared as private by default.
Example
[c]
#include <iostream>
using namespace std;
class Triangle
{
public:
double base;
void setHeight( double hei );
double getHeight( void );
private:
double height;
};
// Member functions definitions
double Triangle::getHeight(void)
{
return height ;
}
void Triangle::setHeight( double hei )
{
height = hei;
}
// Main function for the program
int main( )
{
Triangle tri;
// set box base without member function
tri.base = 15.0; // OK: because base is public
cout << "base of box : " << tri.base <<endl;
// set box width without member function
// tri.height = 10.0; // Error: because height is private
tri.setHeight(25.0); // Use member function to set it.
cout << "height of box : " << tri.getHeight() <<endl;
return 0;
}[/c]
Output
[c]
base of box : 15
height of box : 25[/c]
protected
Description
The member of a class with protected access specifier is not accessible and viewed outside of the class same as private access specifier.But, the advancement in protected is, it can be access by the child class members otherwise called as derived classes. Derived class is nothing but sub class of a class.
Example
[c]
#include <iostream>
using namespace std;
class Pyramid
{
protected:
double base;
};
class Triangle:Pyramid // SmallBox is the derived class.
{
public:
void setSmallBase( double bas );
double getSmallBase( void );
};
// Member functions of child class
double Triangle::getSmallBase(void)
{
return base ;
}
void Triangle::setSmallBase( double bas )
{
base = bas;
}
// Main function for the program
int main( )
{
Triangle pyramid;
// set box width using member function
pyramid.setSmallBase(25.0);
cout << "Base of pyramid : "<< pyramid.getSmallBase() << endl;
return 0;
}[/c]
Output
[c]
Base of pyramid : 25
[/c]