Access Specifiers

Access specifiers in C++ are keywords used to control the visibility and accessibility of class members. They help protect data and support the concept of encapsulation in Object-Oriented Programming (OOP).

What are Access Specifiers?

Access specifiers define who can access variables and functions inside a class.

They are used to control data security and program structure.

Types of Access Specifiers in C++

C++ provides three main access specifiers:

  • Public
  • Private
  • Protected

1. Public Access Specifier

Public members can be accessed from anywhere in the program.

Example of Public Access

#include <iostream>
using namespace std;

class Student {

public:

string name;

void display() {

cout << name;
}
};

int main() {

Student s1;

s1.name = "Ali";

s1.display();

return 0;
}

Output

Ali

2. Private Access Specifier

Private members can only be accessed inside the class. They cannot be accessed directly from outside the class.

Example of Private Access

#include <iostream>
using namespace std;

class Student {

private:

string name;

public:

void setName(string n) {

name = n;
}

void display() {

cout << name;
}
};

int main() {

Student s1;

s1.setName("Ahmed");

s1.display();

return 0;
}

Output

Ahmed

3. Protected Access Specifier

Protected members are similar to private members, but they can also be accessed in derived classes through inheritance.

Example of Protected Access

#include <iostream>
using namespace std;

class Base {

protected:

int number;
};

class Derived : public Base {

public:

void setValue() {

number = 50;
}

void show() {

cout << number;
}
};

int main() {

Derived d1;

d1.setValue();

d1.show();

return 0;
}

Output

50

Default Access Specifier

In a class, if no access specifier is written, the default is private.

class Test {

int x; // private by default
};

Difference Between Access Specifiers

Access SpecifierAccessible Inside ClassAccessible Outside ClassAccessible in Derived Class
PublicYesYesYes
PrivateYesNoNo
ProtectedYesNoYes

How Access Specifiers Work

  • Public members are open to all
  • Private members are hidden from outside
  • Protected members allow controlled inheritance access

Real-Life Example

Think of a bank account:

  • Account balance = private
  • Deposit and withdraw functions = public
  • Special banking features for branch systems = protected

This protects sensitive information while allowing controlled access.

Advantages of Access Specifiers

  • Protect sensitive data
  • Improve code security
  • Support encapsulation
  • Control data access
  • Make programs more organized

Why Access Specifiers are Important

Access specifiers are important because they:

  • Prevent unauthorized access
  • Improve program reliability
  • Support secure programming
  • Help create modular applications

Applications of Access Specifiers

Access specifiers are widely used in:

  • Banking software
  • Student management systems
  • Medical applications
  • Large enterprise software
  • Secure data handling systems

Conclusion

Access specifiers in C++ control how class members are accessed inside and outside a class. Public, private, and protected access levels help improve data security, program organization, and encapsulation, making them essential for Object-Oriented Programming.

Home » Advanced C++ > Object-Oriented Programming > Access Specifiers