Access Modifiers in C++
Access Modifiers in C++
Access modifiers are used to implement an important aspect of Object-Oriented Programming known
as Data Hiding.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class
members, i.e., they set some restrictions on the class members so that they can’t be directly accessed
by the outside functions.
1. Public
2. Private
3. Protected
Note: If we do not specify any access modifiers for the members inside the class, then by default the
access modifier for the members will be Private.
1. Public: All the class members declared under the public specifier will be available to everyone.
The data members and member functions declared as public can be accessed by other classes and
functions too. The public members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.
Example:
// AccessModifire.cpp
// EXAMPLE01: C++ program to demonstrate public access modifier
#include<iostream>
using namespace std;
// class definition
class Circle {
public:
double radius;
double compute_area() {
return 3.14 * radius * radius;
}
};
// main function
int main() {
Circle obj;
// accessing public data member outside class
obj.radius = 5.5;
Output:
In the above program, the data member radius is declared as public so it could be accessed
outside the class and thus was allowed access from inside main().
NAVANIT CHOUDHURY Page 1
Access Modifiers in C++
2. Private: The class members declared as private can be accessed only by the member
functions inside the class.
They are not allowed to be accessed directly by any object or function outside the class. Only
the member functions or the friend functions are allowed to access the private data members
of the class.
Example:
// PrivateAccessModifier.cpp
// EXAMPLE02: C++ program to demonstrate private access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// main function
int main()
{
// creating object of the class
Circle obj;
Output:
The output of the above program is a compile time error because we are not allowed to access the
private data members of a class directly from outside the class. Yet an access to obj.radius is
attempted, but radius being a private data member, we obtained the above compilation error.
Example:
// PrivateAccessModifier_Modified.cpp
// EXAMPLE03: C++ program to demonstrate private access modifier
#include<iostream>
using namespace std;
class Circle {
private:
double radius; // private data member
public:
void compute_area(double r) { // public member function
// member function can access private data member radius
radius = r;
// main function
int main() {
// creating object of the class
Circle obj; // object
// trying to access private data member directly outside the class
obj.compute_area(1.5);
return 0;
}
Output:
3. Protected: The protected access modifier is similar to the private access modifier in the sense that
it can’t be accessed outside of its class unless with the help of a friend class. The difference is that the
class members declared as Protected can be accessed by any subclass (derived class) of that class as
well.
Note: This access through inheritance can alter the access modifier of the elements of base class in
derived class depending on the mode of Inheritance.
Example:
// ProtectedAccessModifier.cpp
// EXAMPLE04: C++ program to demonstrate protected access modifier
#include <bits/stdc++.h>
using namespace std;
// base class
class Parent {
protected:
int id_protected; // protected data members
}; //end of class Parent
void displayId() {
cout << "\n\t id_protected is : " << id_protected << endl;
}
}; //end of sub class Child
// main function
int main() {
Child obj1;
// member function of the derived class can access
// the protected data members of the base class
obj1.setId(81);
obj1.displayId();
return 0;
}
Output:
id_protected is: 81