Access Specifiers in C++

Last Updated : 9 May 2026

Access specifiers in C++ are used to control the accessibility of class members such as variables and functions. They help achieve encapsulation by restricting direct access to internal data and implementation details.

In this chapter, you will learn about access specifiers in C++, their types, and how they control access to class members.

What are Access Specifiers in C++?

In C++, access specifiers define how class members can be accessed from outside the class. They are used to control the visibility and accessibility of variables and functions in a class.

Access specifiers help protect data from unauthorized access and hide internal implementation details.

They ensure that only the required parts of a class are accessible, while sensitive or internal details remain hidden.

There are mainly three types of access specifiers:

  • Public
  • Private
  • Protected
Access Specifiers in C++

Here, we will discuss these access specifiers one by one.

Public Access Specifier

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:

Example

Let us take an instance to illustrate the public access specifier in C++.

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:

Example

Let us take an instance to illustrate the private access specifier in C++.

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:

Example

Let us take an example to illustrate the protected access specifier in C++.

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.

Comparison of Access Specifiers

The following table shows the accessibility of class members for different access specifiers in C++.

Access SpecifiersSame ClassDerived ClassOutside Class
Public Access SpecifierYesYesYes
Private Access SpecifierYesNoNo
Protected Access SpecifierYesYesNo

Next TopicC++ Templates