Public, private and protected access modifiers in C++

Updated

One of the most important concepts in OOPS is protected access modifiers ,you know there are instances when protecting your identity, your data is your at most priority . we use private access modifiers in class function to protect our data for any tempering but it does not allow its inheritance from its parent class to other class but if you have a public access modifiers than it can be inherited and can be tempered as well so they are generally not very important but what if your data is very sensitive and you to inherit it too. than there comes protected access modifiers they can be inherited but cannot be tempered in below code you would understand usage of public, private and protected access modifiers.

A class box with private, protected and public members and a grid showing which of them a derived class and outside code can reach.

Public Access Modifier in C++ :

All the variables and functions declared under public access modifier will be available for everyone. They can be accessed both inside and outside the class. Dot (.) operator is used in the program to access public data members directly.

Private Access Modifier in C++ :

All the variables and functions declared under a private access modifier can only be used inside the class. They are not permissible to be used by any object or function outside the class.

An example program to demonstrate classes, public and private access modifiers are shown in Code Snippet 1.

#include<iostream>
using namespace std;

class Employee
{
    private:
        int a, b, c;
    public:
        int d, e;
        void setData(int a1, int b1, int c1); // Declaration
        void getData(){
            cout<<"The value of a is "<<a<<endl;
            cout<<"The value of b is "<<b<<endl;
            cout<<"The value of c is "<<c<<endl;
            cout<<"The value of d is "<<d<<endl;
            cout<<"The value of e is "<<e<<endl;
        }
};

void Employee :: setData(int a1, int b1, int c1){
    a = a1;
    b = b1;
    c = c1;
}

int main(){
    Employee W3Colleges;
    // W3Colleges.a = 134; -->This will throw error as a is private
    W3Colleges.e = 89;
    W3Colleges.d = 34;
    W3Colleges.setData(1,2,4);
    W3Colleges.getData();
    return 0;
}
Output

The value of a is 1
The value of b is 2
The value of c is 4
The value of d is 34
The value of e is 89

As shown in Code Snippet 1, 1st we created an “employee” class, 2nd three integer variables “int a”, “int b”, and “int c” were declared under the private access modifier, 3rd two integer variables “int d” and “int e” was declared under the public access modifiers, 4th “setData” function was declared, 5th “getData” function was defined and values of all the variables are printed. 6th “setData” function was defined outside the “employee” class by using a scope resolution operator; “setData” function is used to assign values to the private member of the class. An example to create the object of the class and use its class members is shown in Codesnippet 1

As shown in Code Snippet 1, 1st we created an object “W3Colleges” of the class “employee”; 2nd we assigned values to “int d” and “int e” which are public class members. If we try to assign values to the private class member’s compiler will throw an error. 3rd we passed the values to the function “setData” and at the end, we called “getData” function which will print the values of all the variables. The output for the following program is shown in Output tab above

Protected Access Modifiers in C++ :

All the variables and functions declared under a protected access modifier can be used in both inside and in other class also through in inheritance

In below table it is mentioned how public private and protected access modifiers work when inherited

Public derivation Private derivation Protected derivation
Private members Not Inherited Not Inherited Not Inherited
Protected members  Protected Private  Protected
Public members  Public Private Protected

An example program to demonstrate classes, protected access modifiers are shown in Code Snippet 2.

#include<iostream>
using namespace std;
class Base{
    protected:
        int a; 
    private:
        int b;
};
class Derived: protected Base{
public:
    Derived(int x){
      a=x;
      cout << "derived class's constructor is called with variables " << a << endl;
    }
};
int main(){
    Base b;
    Derived d(5);
    // cout<<d.a; // Will not work since a is protected in both base as well as derived class
    return 0;
}
Output

derived class's constructor is called with variables 5
Comment
Mithlesh Upadhyay Published 16 Nov 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Topics:

Related tutorials