Introduction
In object-oriented programming, C++ stands out for its robustness and versatility. One of the fundamental concepts that underpin the security and integrity of C++ programs is access control, managed through access specifiers. Access specifiers define the level of accessibility and visibility for class members, ensuring encapsulation—a core principle of object-oriented design. In C++, three primary access specifiers exist: public, private, and protected.

Also see, Literals in C.Fibonacci Series in C++
What Are Access Specifiers In C++?
In C++, access specifiers are keywords used to define the accessibility and visibility of class members (data members and member functions) from outside the class. They determine how the members of a class can be accessed by other parts of the program. C++ provides three access specifiers:
1. Public:
- Members declared as public are accessible from anywhere in the program, both inside and outside the class.
- Public members can be accessed directly using the dot (.) operator with an object of the class.
- Public members define the interface of the class and are used to provide access to the class's functionality to other parts of the program.
2. Private:
- Members declared as private are only accessible within the same class.
- Private members cannot be accessed from outside the class, neither by other classes nor by functions that are not members of the class.
- Private members are used to encapsulate the internal state and implementation details of a class, hiding them from external access.
- By convention, data members are usually declared as private to ensure data encapsulation and prevent unauthorized access or modification.
3. Protected:
- Members declared as protected are accessible within the same class and its derived classes (subclasses).
- Protected members cannot be accessed from outside the class hierarchy.
- Protected members are used when you want to allow derived classes to access certain members of the base class while still restricting access from other parts of the program.
- Protected members provide a level of encapsulation between public and private, allowing inheritance and extending the functionality of a class.
For example:
class MyClass {
public:
void publicFunction() {
// Accessible from anywhere
}
private:
int privateVariable;
void privateFunction() {
// Accessible only within the class
}
protected:
void protectedFunction() {
// Accessible within the class and its derived classes
}
};
In this example:
- `publicFunction()` is a public member function that can be accessed from anywhere in the program using an object of `MyClass`.
- `privateVariable` and `privateFunction()` are private members that can only be accessed within the `MyClass` itself.
- `protectedFunction()` is a protected member function that can be accessed within `MyClass` and its derived classes.
Note: Access specifiers play a crucial role in encapsulation and controlling the accessibility of class members. They help in achieving data hiding, preventing unauthorized access, and providing a clear interface for interacting with objects of a class.