Access modifiers in OOP
Data hiding restricts access to the data member of the class, which is one of the popular features of Object Oriented Programming (OOP). Everything that comprises a class has an access modifier that determines its scope.
Access modifiers prevent data members or functions of one class from tampering with another class while restricting its access. It allows us to select which members can be accessed directly by outside functions and which are not. This answer will discuss access modifiers and their implementation in C++.
Types of access modifiers
C++ is an object-oriented programming language that has three types of access modifiers:
Public
Private
Protected
Note: If any access modifiers is not specified, by default, the access modifier for the members will be private.
Let's see each one of these access modifiers in detail.
Public
The class members declared under the public keyword are available to everyone. They are accessible to any part of the program. It is the most widely used access specifier. Let's see an example of this access specifier:
#include<iostream>using namespace std;// class definitionclass square{public:double length;double compute_area(){return length*length;}};// main functionint main(){square s1;// accessing public datamember outside classs1.length = 5.5;cout << "Length is: " << s1.length << "\n";cout << "Area is: " << s1.compute_area();return 0;}
In the code above, the data member length is declared as public so we can access it outside the class and therefore is allowed access from inside the main().
Note: The dot member access operator
(.)with the object of the class is used to access class members.
Private
The class members declared under the private keywords are only available inside the class. They are not accessible to any function or object outside the class. Only
However, we can indirectly access private data members by using them in public data. Let's see the example given below:
#include<iostream>using namespace std;// class definitionclass square{private:double length;public:double compute_area(double l){length=l;return length*length;}};// main functionint main(){square s1;double l = 5.5;cout << "Length is: " << l << "\n";//accessing public data membercout << "Area is: " << s1.compute_area(l);return 0;}
In the above code, we indirectly accessed the private data member length using the public data member.
Note: When we try to access private data members outside the class, the compiler throws an
error: ‘double square::length’ is privateerror.
Protected
The class members declared under the protected keyword are available inside the class and in its child class. They are not accessible to any function or object outside the class. Only
#include <iostream>using namespace std;// declare parent classclass parent {// protected elementsprotected:int id;};// declare child classclass child : public parent {public:void displayId(int i) {id = i;cout << "ID = " << id << endl;}};int main() {int ID=1419;// declare object of child classchild object;// call child class function pass id as argumentobject.displayId(ID);return 0;}
In the above code, the member function of the derived class child can access the protected data members, for example, the id of the base class Parent.
Note: Derived classes can only access
protectedandpublicdata members.
Conclusion
In this answer, we learn about access modifiers in C++, but these modifiers' meanings can vary depending on the language. Their visibility can vary from the same class to the package where the class is specified to general access permission.
Free Resources