22 Jan C++ Access Specifiers
If you want to learn how to access the members of a class in C++, then understanding the Access Specifiers is a must. The members of a class are private by default, therefore if you will not add any access specifier, then the specifier is private.
Types of Access Specifiers
There are three Access Specifiers in C++:
- public
- private
- protected
The following is the syntax to declare access specifiers in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class ClassName { private: // The private members/methods public: // The public members/methods protected: // The protected members/methods }; |
The public Access Specifier
As the name suggests, if the access specifiers are public, the members can be accessed from outside the class easily. Let’s see the syntax:
1 2 3 4 5 6 7 8 |
class ClassName { // The access specifier is public public: int variable_name; } |
Let us see an example to implement the public access specifier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; class Rectangle { // The public access specifier public: double length; double width; }; int main() { // Create an object Rectangle rct; // Access the length and width attributes // Set the values rct.length = 25; rct.width = 50; // Display the length and width values cout <<"Length = "<<rct.length; cout <<"\nWidth = "<<rct.width; return 0; } |
Output
1 2 3 4 |
Length = 25 Width = 50 |
The private Access Specifier
If the access specifiers are private, the members cannot be accessed from outside the class. Let’s see the syntax:
1 2 3 4 5 6 |
class ClassName { // The access specifier is private int variable_name; } |
As you can see above, if you won’t set any specifier, it is private by default.
Let us now see the above example and try to understand the private access specifier in C++. We will set one of the attributes to private:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> using namespace std; class Rectangle { // The public access specifier public: double length; // The public attribute // The private access specifier private: double width; // The private attribute }; int main() { // Create an object Rectangle rct; // Access the length and width attributes // Set the values rct.length = 25; /* This will give an error since private members cannot be accessed from outside the class */ rct.width = 50; return 0; } |
The output is as follows. Displays an error since we are trying to access the width attribute, which is private:
1 2 3 4 5 6 7 8 9 |
prog.cpp: In function ‘int main()’: prog.cpp:26:7: error: ‘double Rectangle::width’ is private within this context 26 | rct.width = 50; | ^~~~~ prog.cpp:12:12: note: declared private here 12 | double width; // The private attribute | ^~~~~ |
The protected Access Specifier
If the access specifier is set protected, the protected members can be accessed from the derived/ child class. The concept of Inheritance i.e. derived and child class is discussed in the Inheritance lesson.
No Comments