Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Access Modifiers in C++: Public, Private and Protected

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

11 Jun 2024
Advanced
10.4K Views
10 min read
Learn with an interactive course and practical hands-on labs

Free C++ Course: Learn C++ In 21 Days

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++ Certification Course.

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)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this