Access Modifiers in C++: Public, Private and Protected

Access Modifiers in C++: Public, Private and Protected

26 Jul 2025
Advanced
16.4K Views
10 min read
Learn with an interactive course and practical hands-on labs

Free C++ Online Course With Certificate

Access Specifiers in C++: An Overview

In our journey of understanding OOPs concepts in C++, let's look at a technique of data hiding. We are very well aware that data hiding is one of the main characteristics of object-oriented programming languages. It assists the program in showing the essential features without showing the functionality or the details of the program to its users. In this C++ tutorial, we will see what is an Access Specifier/Access Modifier in C++ and its types. For more information, enroll in our C++ Online Course Free.

What are Access Specifiers in C++?

Access specifiers define how the members (data members and member functions) of a class can be accessed. They allow us to determine which class members are accessible to other classes and functions, and which are not.

Types of Access Modifiers in C++

There are three access specifiers in C++:

  1. public
  2. private
  3. protected

We will one by one look at them below.

  1. Public Access Modifier in C++

This employs a public keyword to create public members (data and functions). The public members are accessible from any part of the program i.e. from inside or outside the class.

Example of C++ Public Access specifier in C++ Compiler


#include <iostream>
using namespace std;
class ScholarHat {
 public: // Public access specifier
 int x; // Public attribute

 void display() { //// Public method
 cout << "x = " << x << endl;
 }

};

int main() {
 ScholarHat myObj;
 myObj.x = 70; 
 myObj.display();
 return 0;
}
  • Here, we have created a class named ScholarHat, which contains a public variable x and a public function display().
  • In main(), we have created an object of the ScholarHat class named myObj.
  • We then access the public elements directly by using the codes myObj.x and myObj.display().

Output

x = 70

  1. Private Access Modifier in C++

This employs a private keyword to create private members (data and functions). The private members can only be accessed from within the class.

Example of C++ private Access specifier


#include <iostream>
using namespace std;
class ScholarHat {
 private: // Private access specifier
 int y; //private attribute
 public: // Public access specifier
 int x; // Public attribute

 void display() { // Public method
 cout << "y = " << y << ", x = " << x << endl;
 }

};

int main() {
 ScholarHat myObj;
 myObj.x = 70; 
 myObj.y = 80
 myObj.display();
 return 0;
}

Here, y is a private member. It cannot be accessed outside the class. Therefore the program shows an error on compilation.

Output

prog.cpp: In function ‘int main()’:
prog.cpp:18:9: error: ‘int ScholarHat::y’ is private within this context
 18 | myObj.y = 70;
 | ^
prog.cpp:5:9: note: declared private here
 5 | int y; //private attribute
 | ^
  • The above error can be corrected if we access the private member y of the class ScholarHat using a public method inside the same class e.g.display().
    
    #include <iostream>
    using namespace std;
    
    class ScholarHat {
    private:
     int y; // Private attribute
    public:
     int x; // Public attribute
    
     void display(int a) { // Public method
     y = a;
     cout << "y = " << y << ", x = " << x << endl;
     }
    };
    
    int main() {
     ScholarHat myObj;
     myObj.x = 70;
     myObj.display(80);
     return 0;
    }
    

    Here, when we call the public method display() using the object myObj, we pass an argument for the private member y. The method display() assigns the value of parameter a to y and then displays it.

    Output

    y = 80, x = 70
    
  • The private as well as public members can be initialized using constructors which we will learn in the next section, Constructors and destructors in C ++

Remember: By default, all members of a class in C++ are private if you don't specify an access specifier.

  1. Protected Access Modifier in C++

This employs the protected keyword to create protected members (data and function). The protected members can be accessed within the class and from the derived class.

We will see this in detail in the section, Inheritance in C++

Read More - C++ Interview Interview Questions and Answers

Access Modifiers in C++

Access SpecifiersSame ClassDerived ClassOutside Class

public

YesYesYes

private

YesNoNo

protected

YesYesNo
Summary

We saw what are access specifiers in C++, their types, how they are used, why they are used, etc. In further tutorials on OOP concepts, we will see their wide applications in programming. For more understanding, check our C++ Certification.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure DevOps Certification Training
24 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this