In C++, access specifiers are mainly utilized to define the accessibility of the class member (such as variables and functions ) from outside the class. These are very helpful for encapsulation by restricting unauthorized or unintended access to internal data and implementation details.
Access specifiers allow us to control access to specific components of the class from outside. It also helps to protect the data from being unintentionally changed or misused. It ensures that only the parts of the class meant to be used by other parts of the program are shown, while sensitive or internal details remain hidden. It also helps to enhance security, robustness, and code modularity.
Types of Access Specifiers
In C++ , access specifiers have mainly three types that can help to define how class members can be accessed:
Here, we will discuss these access specifiers one by one.
Public Access Specifier
In C++, the public access specifier is mainly used to specify that a class member can be accessed from anywhere inside and outside the class. It means that any function or object can access the public members of the class. The public members of a class are mainly utilized to represent the class interface of the class.
If we declare a member function using a public keyword, it indicates that there are no restrictions on its access. It is commonly used when data or functions must be easily shared or accessed by other parts of the code.
Syntax
It has the following syntax:
class Class_Name {
public:
// Public data members
int public_var;
// Public member functions
void publicFunc();
};
C++ Public Access Specifier Example
Let us take an instance to illustrate the public access specifier in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class Book_Store //base class
{
public:
string booktitle;
void showTitle()
{
cout << "The Title of Book is: " << booktitle << endl;
}
};
int main() //main function
{
Book_Store book; //object
book.booktitle = "The C++ Programming Guide";
book.showTitle();
return 0;
}
Output:
The Title of Book is: The C++ Programming Guide
Explanation:
In this example, the public access specifier is used by the class BookStore to allow unrestricted access to its booktitle attribute and showTitle method. In the main function, we assign a value to the booktitle and then invoke the method to display it.
Private Access Specifier
In C++, the private access specifier is mainly utilized to restrict access to the class members. Whenever we declare a member function as private (using the private keyword), it can only be accessed and changed by the member functions of the same class. It does not allow direct access by any function or object outside the class. It ensures data hiding that allows us to protect the internal state of an object from unintended or unauthorized modifications.
When we use private members in a program, a class may encapsulate its internal details, which display the necessary details and functionality to the outside world using public methods. It helps to maintain data integrity and control over how the class operates.
Syntax
It has the following syntax:
class Class_name {
private:
int privateVar; // Private data members
void private_Func(); // Private member functions
};
C++ Private Access Specifier Example
Let us take an instance to illustrate the private access specifier in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class BankAccount //base class
{
private: //private specifiers
double balance;
public:
string account_holder_name;
void setBalance(double amount)
{
balance = amount;
cout << "The Account Holder name is: " << account_holder_name << "\nThe remaining amount is: $" << balance << endl;
}
};
int main() //main function
{
BankAccount a; //object
a.account_holder_name = "john";
a.setBalance(1380.27);
return 0;
}
Output:
The Account Holder name is: John
The remaining amount is: $1380.27
Explanation:
In this example, we demonstrate the use of the private access specifier by limiting direct access to the balance variable that can only be altered and viewed using the setBalance public method.
Protected Access Specifier
In C++, the protected access specifier is mainly utilized to specify that a class member can be accessed within the class itself and by its derived classes. It means that any function or object outside the class cannot access the protected members of the class. The protected members of a class are typically used to represent the implementation of a class that must be accessible to its derived classes.
Syntax
It has the following syntax:
class Class_name {
protected:
// Protected data members
int protectedVar;
// Protected member functions
void protected_Func();
};
C++ Protected Access Specifier Example
Let us take an example to illustrate the protected access specifier in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class Employee //Base class
{
protected:
double salary;
public:
string name;
void setSalary(double amount)
{
salary = amount;
}
};
class Manager : public Employee //derived class
{
public:
void Display()
{
cout << "The Manager Name is: " << name << "\nThe Salary is: $" << salary << endl;
}
};
int main() //main function
{
Manager m;
m.name = "John";
m.setSalary(95000.00);
m.Display();
return 0;
}
Output:
The Manager Name is: John
The Salary is: $95000
Explanation:
In this example, we demonstrate the use of access specifiers. Here, we have taken an Employee class that contains a protected salary and a public name, with a setSalary method to assign the salary. After that, the Manager class inherits from Employee and accesses the protected salary directly in its Display method. In the main function, we create a Manager object, set the name and salary, and show the information.
Access Modifiers in C++
Here is a table that shows the access modifiers in C++.
| Access Specifiers | Same Class | Derived Class | Outside Class |
|---|---|---|---|
| Public Access Specifier | Yes | Yes | Yes |
| Private Access Specifier | Yes | No | No |
| Protected Access Specifier | Yes | Yes | No |
Conclusion
In conclusion, access specifiers are fundamental concepts to object-oriented programming as they control how class members are accessed and modified. These specifiers are very helpful in encapsulation, which helps to ensure data security, modularity, and code maintainability.
C++ Access Specifiers MCQs:
1) Which of the following access specifiers allows access to members from outside the class?
- private
- protected
- public
- static
2) In C++, what is the default access specifier for class members?
- private
- static
- public
- protected
3) Which of the following access specifiers only enables access to the class and its derivative classes?
- public
- private
- static
- protected
4) What happens if we try to access a private member from outside the class in C++?
- It will work normally
- It will issue a warning
- It will result in a compile-time error
- It will switch to public
5) Which of the following access specifiers is the least accessible?
- public
- private
- protected
- virtual