In this article, you will learn about C++ access specifiers with the help of programming examples and explanations. The access specifiers of C++ are public, private, and protected.
Basically, the access specifier defines the access rules for class members. This rule is applicable until the end of the class or until another access specifier is encountered. Let’s see a few examples,
Example-1:
You can see there is no other access specifier after the public specifier in class X. So all members of the class after the public specifier becomes public.
class X { public: int a; // X::a is public int b; // X::b is public int c; // X::c is public };
Example-2:
You can see I have used the public access specifier after the private access specifier in class Y. So all members of the class after the private specifier and before meeting the public specifier become private.
class Y { private: int a; // Y::a is private public: int b; // Y::b is public int c; // Y::c is public };
You can use any number of access specifiers in your class in any order. That means there is any number of access specifiers allowed in any order. See the below example,
struct S { int a; // S::a is public by default: struct used protected: int b; // S::b is protected private: int c; // S::c is private public: int d; // S::d is public };
Default access specifier of C++ Class and Struct:
By default members of the struct is public while in a class they are private. Let’s see example codes to understand the statement,
class A { int data; // By default data is private }; struct B { int aata; // By default data is public };
Still, if you are not able to understand any of the above statements, don’t worry you will understand it. I will explain each access control specifier with the help of programming examples.
Summary:
Members of a class defined with the keyword “class” are private by default. Members of a class defined with the keywords “struct” or “union” are public by default.
Structures | Classes | Unions |
---|---|---|
class key is struct |
class key is class |
class key is union |
Default access is public | Default access is private | Default access is public |
No usage constraints | No usage constraints | Use only one member at a time |
Types of C++ Access Specifier:
- private access specifier.
- protected access specifier.
- public access specifier.
So let’s understand all three access specifiers (public, protected, and private) one by one with examples.
private access specifier:
Members (either data member or member function) declared in a private section of a class can only be accessed by members and friends of that class. This applies to all members declared up to the next access specifier or the end of the class.
Example:
class X { private: // Access only by members //and friends of that class int a; };
Some Programming Examples of C++ private access specifiers:
Let’s see some programming example and their explanation for a better understanding.
Example-1:
The following example illustrates that we can not access private members directly from class objects.
#include <iostream> using namespace std; //X is class class X { private: int data; }; int main() { //Object of the class X X obj; //Accessing data with object of class X obj.data =10; return 0; }
Output:
Explanation:
The ‘data’ is a private member which is why when trying to access it with a class object compiler throws the error.
Example-2:
The following example illustrates how a member function uses to access the private members of the class.
#include <iostream> using namespace std; //X is class class X { public: //public methods of class X void setValue(int value) { data = value; } int getValue() { return data; } private: int data; }; int main() { //Object of the class X X obj; //setting value obj.setValue(10); //getting the value cout<< obj.getValue()<<endl; return 0; }
Output:
10
Explanation:
Both setValue and getValue are the member functions so they allow to access the private member of class X.
Example-3:
The following example illustrates how a friend function uses to access the class’s private members.
#include <iostream> using namespace std; //X is class class X { //a is private members int a; friend void friend_set(X*, int); public: int getValue(); }; //getting value of a int X::getValue() { return a; } //setting the value using friend function void friend_set(X* p, int value) { p->a = value; } int main() { //object of class X X obj; //setting the value friend_set(&obj,10); //getting the value of a cout << obj.getValue()<<endl; return 0; }
Output:
10
protected access specifier:
Members (either data member or member function) declared in a protected section of a class can only be accessed by class members and friends of that class, by classes derived from that class, and by their friends. This applies to all members declared up to the next access specifier or the end of the class.
class X { protected: //Access by members and friends of that class, //and by members and friends of derived classes. int a; };
Some Programming Examples of C++ protected access specifiers:
Let’s see some programming example and their explanation for a better understanding.
Example-1:
The following example illustrates that we can not access protected members directly from class objects.
#include <iostream> using namespace std; //X is class class X { protected: //protected member int data; }; int main() { //Object of the class X X obj; //Accessing data with object of class X obj.data =10; return 0; }
Output:
Explanation:
The ‘data’ is a protected member which is why when trying to access it with a class object compiler throws the error.
Example-2:
The following example illustrates how a member function uses to access the private members of the class.
#include <iostream> using namespace std; //X is class class X { public: //public methods of class X void setValue(int value) { data = value; } int getValue() { return data; } protected: int data; }; int main() { //Object of the class X X obj; //setting value obj.setValue(100); //getting the value cout<< obj.getValue()<<endl; return 0; }
Output:
100
Explanation:
Both setValue and getValue are the member functions so they allow to access the protected member of class X.
Example-3:
The following example illustrates how a friend function uses to access the protected members of the class.
#include <iostream> using namespace std; //X is class class X { protected: //a is protected members int a; friend void friend_set(X*, int); public: int getValue(); }; //getting value of a int X::getValue() { return a; } //setting the value using friend function void friend_set(X* p, int value) { p->a = value; } int main() { //object of class X X obj; //setting the value friend_set(&obj,200); //getting the value of a cout << obj.getValue()<<endl; return 0; }
Output:
200
Example-4:
The following example illustrates how derived class members access the protected members of the base class.
#include <iostream> using namespace std; //X is class class X { //data is protected members protected: int data; }; //Child class of X class Child_X : public X { public: //public methods of class Child_X void setValue(int value) { //data is protected member of base class data = value; } int getValue() { return data; } }; int main() { //Object of the class X Child_X obj; //setting value obj.setValue(100); //getting the value cout<< obj.getValue()<<endl; return 0; }
Output:
100
public access specifier:
Member (either data member or member function) declared in a public section of a class can be accessed by anyone. This applies to all members declared up to the next access specifier or the end of the class.
Example:
class X { public: // Access by anyone int a; };
Example-1:
The following example illustrates how an object accesses the public members of the class.
#include <iostream> using namespace std; //X is class class X { //data is public members public: int a; }; int main() { //Object of the class X X obj; //setting value obj.a= 100; //getting the value cout<< obj.a<<endl; return 0; }
Output: 100
Recommended Articles for you:
- C++ Programming Courses And Tutorials.
- Encapsulation in C++.
- Inheritance in C++ with example code.
- Polymorphism in C++ for example.
- Compile Time Polymorphism with Templates in C++.
- Interview questions on the virtual keywords in C++.
- C++ Interview Questions and Answers.
- Operator Overloading in C++ with some FAQ.
- Introduction of reference in C++.
- Use of mutable keywords in C++.