Month End Sale: Get Extra 10% OFF on Job-oriented 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
6.08K Views
10 min read
Learn via Video Course & by Doing Hands-on Labs

Free C++ Course Online with Certificate [Full Course]

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

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Generative AI For Software Developers Jul 27 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

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
  • 800+ Hands-On Labs
  • 400+ 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