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

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

20 Apr 2024
Advanced
3.69K Views
9 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

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
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this