Access modifiers in OOP

Share

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:

  1. Public

  2. Private

  3. Protected

Relations of access modifiers

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 definition
class square
{
public:
double length;
double compute_area()
{
return length*length;
}
};
// main function
int main()
{
square s1;
// accessing public datamember outside class
s1.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 friend A friend function is a function that is specified outside a class but has the ability to access the class members' protected and private dataor member functions can access the class's private data members.

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 definition
class square
{
private:
double length;
public:
double compute_area(double l)
{
length=l;
return length*length;
}
};
// main function
int main()
{
square s1;
double l = 5.5;
cout << "Length is: " << l << "\n";
//accessing public data member
cout << "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 private error.

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 the friend classA friend function can access the private and protected data of a class. can access the protected data members outside the class.

#include <iostream>
using namespace std;
// declare parent class
class parent {
// protected elements
protected:
int id;
};
// declare child class
class child : public parent {
public:
void displayId(int i) {
id = i;
cout << "ID = " << id << endl;
}
};
int main() {
int ID=1419;
// declare object of child class
child object;
// call child class function pass id as argument
object.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 protected and public data 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.

Copyright ©2024 Educative, Inc. All rights reserved