Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of Access Specifiers in C++
3.
Example of Access Specifiers in C++
3.1.
C++
4.
Types of Access Modifiers
4.1.
1. Public Access Modifier
4.1.1.
Example
4.2.
C++
4.3.
2. Private Access Modifier
4.3.1.
Example
4.4.
C++
4.5.
3. Protected Access Modifier
4.5.1.
Example
4.6.
C++
5.
Frequently Asked Questions
5.1.
What is the use of access modifiers in C++?
5.2.
What is the difference between access specifiers in C++?
5.3.
What is the default access specifier?
5.4.
What is the difference between open and public access specifiers?
5.5.
How many public classes a C++ file can have? 
6.
Conclusion
Last Updated: Aug 3, 2024

Access specifiers in C++

Author RAJESH RUNIWAL
2 upvotes

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++

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