22
DecAccess Modifiers in C++: Public, Private and Protected
Access Specifiers in C++: An Overview
In our journey of understanding OOPs concepts in C++, let's look at a technique of data hiding. We are very well aware that data hiding is one of the main characteristics of object-oriented programming languages. It assists the program in showing the essential features without showing the functionality or the details of the program to its users. In this C++ tutorial, we will see what is an Access Specifier/Access Modifier in C++ and its types. For more information, enroll in our C++ Certification Course.
What are Access Specifiers in C++?
Access specifiers define how the members (data members and member functions) of a class can be accessed. They allow us to determine which class members are accessible to other classes and functions, and which are not.
Types of Access Modifiers in C++
There are three access specifiers in C++:
public
private
protected
We will one by one look at them below.
- Public Access Modifier in C++
This employs a public
keyword to create public
members (data and functions). The public
members are accessible from any part of the program i.e. from inside or outside the class.
Example of C++ Public Access specifier in C++ Compiler
#include <iostream>
using namespace std;
class ScholarHat {
public: // Public access specifier
int x; // Public attribute
void display() { //// Public method
cout << "x = " << x << endl;
}
};
int main() {
ScholarHat myObj;
myObj.x = 70;
myObj.display();
return 0;
}
- Here, we have created a class named
ScholarHat
, which contains apublic
variablex
and a public functiondisplay()
. - In
main()
, we have created an object of theScholarHat
class namedmyObj
. - We then access the
public
elements directly by using the codesmyObj.x
andmyObj.display()
.
Output
x = 70
- Private Access Modifier in C++
This employs a private
keyword to create private
members (data and functions). The private
members can only be accessed from within the class.
Example of C++ private Access specifier
#include <iostream>
using namespace std;
class ScholarHat {
private: // Private access specifier
int y; //private attribute
public: // Public access specifier
int x; // Public attribute
void display() { // Public method
cout << "y = " << y << ", x = " << x << endl;
}
};
int main() {
ScholarHat myObj;
myObj.x = 70;
myObj.y = 80
myObj.display();
return 0;
}
Here, y
is a private
member. It cannot be accessed outside the class. Therefore the program shows an error on compilation.
Output
prog.cpp: In function ‘int main()’:
prog.cpp:18:9: error: ‘int ScholarHat::y’ is private within this context
18 | myObj.y = 70;
| ^
prog.cpp:5:9: note: declared private here
5 | int y; //private attribute
| ^
- The above error can be corrected if we access the
private
membery
of the classScholarHat
using apublic
method inside the same class e.g.display()
.#include <iostream> using namespace std; class ScholarHat { private: int y; // Private attribute public: int x; // Public attribute void display(int a) { // Public method y = a; cout << "y = " << y << ", x = " << x << endl; } }; int main() { ScholarHat myObj; myObj.x = 70; myObj.display(80); return 0; }
Here, when we call the public method
display()
using the objectmyObj
, we pass an argument for theprivate
membery
. The methoddisplay()
assigns the value of parametera
toy
and then displays it.Output
y = 80, x = 70
- The
private
as well aspublic
members can be initialized usingconstructors
which we will learn in the next section, Constructors and destructors in C ++
Remember: By default, all members of a class in C++ are private
if you don't specify an access specifier.
- Protected Access Modifier in C++
This employs the protected
keyword to create protected members (data and function). The protected
members can be accessed within the class and from the derived class.
We will see this in detail in the section, Inheritance in C++
Read More - C++ Interview Interview Questions and Answers
Access Modifiers in C++
Access Specifiers | Same Class | Derived Class | Outside Class |
| Yes | Yes | Yes |
| Yes | No | No |
| Yes | Yes | No |
Summary
We saw what are access specifiers in C++, their types, how they are used, why they are used, etc. In further tutorials on OOP concepts, we will see their wide applications in programming. For more understanding, check our C++ Certification.