Access modifiers, also known as Access specifiers, are used to implement Data Hiding, an essential aspect of Object-Oriented Programming. Data hiding is an important feature of Object-Oriented Programming that allows program functions to avoid directly accessing the internal representation of a class type.
The labeled public, private, and protected sections within the class body specify the access restrictions for class members. Access specifiers are the keywords public, private, and protected.
public Members:
Everyone will have access to all class members declared under the public specifier. Other classes and functions can access data members and member functions that have been declared as public. A class’s public members can be accessed from anywhere in the program by using the direct member access operator (.) with the class’s object.
Example:
#include <iostream>
using namespace std;
class Square {
public:
double side;
void setSide( double side );
double getSide( void );
};
// Member functions definitions
double Square::getSide(void) {
return side ;
}
void Square::setSide( double s) {
side = s;
}
// Main function for the program
int main() {
Square sobj;
// set line length
sobj.setSide(6.0);
cout << "Side of Square : " << sobj.getSide() <<endl;
// set line length without member function
sobj.side = 10.0; // OK: because length is public
cout << "Side of Square : " << sobj.side <<endl;
return 0;
}
Output:
Side of Square : 6
Side of Square : 10
private Members:
Outside the class, a private member variable or function cannot be accessed or even viewed.
By default, all members of a class are private. Only member functions or friend functions have access to the class’s private data members.
Example:
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
double compute_area()
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
Output:
/tmp/WglVEdJKPV.cpp: In function 'int main()':
/tmp/WglVEdJKPV.cpp:31:6: error: 'double Circle::radius' is private within this context
31 | obj.radius = 1.5;
| ^~~~~~
/tmp/WglVEdJKPV.cpp:11:10: note: declared private here
11 | double radius;
| ^~~~~~
protected Members:
The protected access modifier is similar to the private access modifier in that it cannot be accessed outside of its class unless a friend class is used. The difference is that Protected class members can be accessed by any subclass (derived class) of that class.
Example:
#include <iostream>
using namespace std;
class Box {
protected:
double width;
};
class SmallBox:Box { // SmallBox is the derived class.
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};
// Member functions of child class
double SmallBox::getSmallWidth(void) {
return width ;
}
void SmallBox::setSmallWidth( double wid ) {
width = wid;
}
// Main function for the program
int main() {
SmallBox box;
// set box width using member function
box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;
return 0;
}
Output:
Width of box : 5
Accessibility of access specifiers in classes during inheritance:
Access Specifiers | Private | Protected | Public |
Access in same class | Yes | Yes | Yes |
Access in derived class | × | Yes | Yes |
Access outside the class | × | × | Yes |
Note: also read about Classes and Objects in C++
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment