Table of contents
1.
Introduction
2.
What Are Access Specifiers In C++?
3.
Syntax of Access Specifiers in C++
4.
Example of Access Specifiers in C++
4.1.
C++
5.
Types of Access Modifiers
5.1.
1. Public Access Modifier
5.1.1.
Example
5.2.
C++
5.3.
2. Private Access Modifier
5.3.1.
Example
5.4.
C++
5.5.
3. Protected Access Modifier
5.5.1.
Example
5.6.
C++
6.
Frequently Asked Questions
6.1.
What is the use of access modifiers in C++?
6.2.
What is the difference between access specifiers in C++?
6.3.
What is the default access specifier?
6.4.
What is the difference between open and public access specifiers?
6.5.
How many public classes a C++ file can have? 
7.
Conclusion
Last Updated: Dec 24, 2024

Access specifiers in C++

Author RAJESH RUNIWAL
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In object-oriented programming, C++ stands out for its robustness and versatility. One of the fundamental concepts that underpin the security and integrity of C++ programs is access control, managed through access specifiers. Access specifiers define the level of accessibility and visibility for class members, ensuring encapsulation—a core principle of object-oriented design. In C++, three primary access specifiers exist: public, private, and protected.

Access specifiers in C++

Also see, Literals in C.Fibonacci Series in C++

What Are Access Specifiers In C++?

In C++, access specifiers are keywords used to define the accessibility and visibility of class members (data members and member functions) from outside the class. They determine how the members of a class can be accessed by other parts of the program. C++ provides three access specifiers:

1. Public:

  • Members declared as public are accessible from anywhere in the program, both inside and outside the class.
  • Public members can be accessed directly using the dot (.) operator with an object of the class.
  • Public members define the interface of the class and are used to provide access to the class's functionality to other parts of the program.

2. Private:

  • Members declared as private are only accessible within the same class.
  • Private members cannot be accessed from outside the class, neither by other classes nor by functions that are not members of the class.
  • Private members are used to encapsulate the internal state and implementation details of a class, hiding them from external access.
  • By convention, data members are usually declared as private to ensure data encapsulation and prevent unauthorized access or modification.

3. Protected:

  • Members declared as protected are accessible within the same class and its derived classes (subclasses).
  • Protected members cannot be accessed from outside the class hierarchy.
  • Protected members are used when you want to allow derived classes to access certain members of the base class while still restricting access from other parts of the program.
  • Protected members provide a level of encapsulation between public and private, allowing inheritance and extending the functionality of a class.

For example: 

class MyClass {
public:
   void publicFunction() {
       // Accessible from anywhere
   }
private:
   int privateVariable;
   void privateFunction() {
       // Accessible only within the class
   }
protected:
   void protectedFunction() {
       // Accessible within the class and its derived classes
   }
};

In this example:

  • `publicFunction()` is a public member function that can be accessed from anywhere in the program using an object of `MyClass`.
  • `privateVariable` and `privateFunction()` are private members that can only be accessed within the `MyClass` itself.
  • `protectedFunction()` is a protected member function that can be accessed within `MyClass` and its derived classes.

Note: Access specifiers play a crucial role in encapsulation and controlling the accessibility of class members. They help in achieving data hiding, preventing unauthorized access, and providing a clear interface for interacting with objects of a class.

Syntax of Access Specifiers in C++

In C++, access specifiers are used within class definitions to control the visibility and accessibility of class members. The three primary access specifiers are public, private, and protected. Their syntax is straightforward:

class ClassName {
public:
    // Members declared here are accessible from anywhere
    int publicVar;
    void publicMethod();

private:
    // Members declared here are accessible only within the class
    int privateVar;
    void privateMethod();

protected:
    // Members declared here are accessible within the class and by derived classes
    int protectedVar;
    void protectedMethod();
};

Example of Access Specifiers in C++

Let's look at a practical example to illustrate how these access specifiers work:

  • C++

C++

#include <iostream>
using namespace std;

class Base {
public:
int publicVar;
void publicMethod() {
cout << "Public Method" << endl;
}

private:
int privateVar;
void privateMethod() {
cout << "Private Method" << endl;
}

protected:
int protectedVar;
void protectedMethod() {
cout << "Protected Method" << endl;
}
};

class Derived : public Base {
public:
void accessMethods() {
publicVar = 1; // Accessible
publicMethod(); // Accessible

// privateVar = 2; // Not accessible
// privateMethod(); // Not accessible

protectedVar = 3; // Accessible
protectedMethod(); // Accessible
}
};

int main() {
Base base;
Derived derived;

base.publicVar = 5; // Accessible
base.publicMethod(); // Accessible

// base.privateVar = 10; // Not accessible
// base.privateMethod(); // Not accessible

// base.protectedVar = 15; // Not accessible
// base.protectedMethod(); // Not accessible

derived.accessMethods(); // Access through derived class
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Public Method
Public Method
Protected Method

In this example:

  • The public members of Base are accessible both inside and outside the class, and from derived classes.
  • The private members are only accessible within the Base class itself.
  • The protected members are accessible within Base and its derived classes but not outside these classes.

Types of Access Modifiers

In C++, there are three kinds of access modifiers:

1. Public access modifier

2. Private access modifier

3. Protected Access modifier

Let us discuss each one of them.

1. Public Access Modifier

Public means all class members declared public might be available to everyone. The data members and member functions said other classes could access the public. Hence, public members can be called from any part of the program.

Example

  • C++

C++

#include <bits/stdc++.h>
using namespace std;
class Sample
{
   public:
   int age;
   void displayAge()
 {
    cout << "Age = " << age << endl;
 }
};
int main()
{
 Sample obj1;
 cout << "Enter your age: ";
 cin >> obj1.age;
 obj1.displayAge();
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Enter your age: 19
Age = 19
You can also try this code with Online C++ Compiler
Run Code

2. Private Access Modifier

The class which was declared as private can only be accessed inside the class where declared. The private class cannot be accessed outside the class and throws a compile-time error. By default, functions and class variables are private.

Example

  • C++

C++

#include <bits/stdc++.h>
using namespace std;
class Sample
{
  private:
   int age;
  public:
   void displayAge(int a)
{
       age = a;
       cout << "Age = " << age << endl;
}
};
int main()
{
   int ageInput;
   Sample obj1;
   cout << "Enter your age: ";
   cin >> ageInput;
   obj1.displayAge(ageInput);
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Enter your age: 19
Age = 19
You can also try this code with Online C++ Compiler
Run Code

3. Protected Access Modifier

Protected is the last access specifier, and it is similar to private. It makes class members inaccessible outside the class, but they can be accessed by any subclass of that class ( Also known as Inheritance ).

Example

  • C++

C++

#include <bits/stdc++.h>
using namespace std;
class Sample
{
  protected:
   int age;
};
class SampleChild: public Sample
{
  public:
   void displayAge(int a)
{
       age = a;
       cout << "Age = " << age << endl;
}
};
int main()
{
   int ageInput;
   SampleChild child;

cout << "Enter your age: ";
   cin >> ageInput;
   child.displayAge(ageInput);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Enter your age: 19
Age = 19

 

Try and compile with online c++ compiler.

Know What is Object in OOPs here in detail.

Frequently Asked Questions

What is the use of access modifiers in C++?

Access Modifiers or access Specifiers in a class are used to assign the accessibility to the class members. that is, it sets some restrictions on the class members not to get directly accessed by the outside functions.

What is the difference between access specifiers in C++?

  • public: Members are accessible from anywhere.
  • private: Members are accessible only within the class.
  • protected: Members are accessible within the class and derived classes.

What is the default access specifier?

The default access specifier for members of a class in C++ is private.

What is the difference between open and public access specifiers?

C++ does not have an "open" access specifier. In languages like Kotlin, "open" allows a class to be inherited. In C++, "public" grants access to members from any part of the program.

How many public classes a C++ file can have? 

In fact, you can not create public classes in a single document, only one class should be public and it should be the name of the class. if you try to create public classes in the same file the compiler generates a compile-time error.

Conclusion

We learned about the access modifiers for C++ classes with the help of examples. And also learned all other types and functions that can access public elements. Public details can be accessed by all other classes and functions, and protected details are just like the private, except derived classes can access them. 

Recommended Reading:

Live masterclass