In C++, Access Specifiers or Access Modifiers are keywords used to control the visibility and accessibility of class members (data members and member functions) from outside the class. They are used to implement Data-Hiding concepts in Object Oriented Programming.
Let's understand them with one real-life example. Imagine you have a remote-controlled car that has three buttons to perform different things:
- Public Buttons: These are the big, colorful buttons on the top that anyone can press. They make the car go forward, backward, left, and right. These are public actions that anyone can control.
- Private Buttons: Inside the remote, some special buttons are hidden. These buttons do things like changing the car's battery or adjusting its internal settings. We don't want just anyone messing with these buttons because it might break the car. Only the person who owns the remote (the car itself) knows about these buttons.
- Protected Buttons: These are a bit like private buttons, but they can be used by a close friend who has a special key. This friend knows a bit more about how the car works and can use these buttons to customize the car safely.
- public.
- private.
- protected.
// C++ example to show public access specifier #include<iostream> using namespace std; // define class class Area{ // access specifier public: int length; int breadth; int calArea(){ return length *breadth; } }; int main(){ Area obj; obj.breadth = 5; obj.length = 10; cout<< "Breadth: " << obj.breadth << endl; cout<< "Length: " << obj.length << endl; cout << "Aread of Rectangle: " << obj.calArea() << endl; return 0; }
Breadth: 5
Length: 10
Aread of Rectangle: 50
// C++ example to show private access specifier #include<iostream> using namespace std; // define class class Area{ // access specifier private: int length; int breadth; int calArea(){ return length * breadth; } }; int main(){ Area obj; obj.breadth = 5; // Error: privateVar is not accessible obj.length = 10; // Error: privateVar is not accessible cout<< "Breadth: " << obj.breadth << endl; cout<< "Length: " << obj.length << endl; cout << "Aread of Rectangle: " << obj.calArea() << endl; return 0; }
//C++ example for protected access specifier #include <iostream> using namespace std; // Base class class Vehicle { protected: int speed; public: Vehicle() : speed(0) {} void setSpeed(int s) { speed = s; cout << "Setting speed to " << speed << " km/h\n"; } }; // Derived class class Car : public Vehicle { public: void showSpeed() { // Derived class can access the protected member 'speed' of the base class cout << "Current speed of the car: " << speed << " km/h\n"; } void accelerate() { // Derived class can modify the protected member 'speed' of the base class speed += 10; cout << "Accelerating! New speed: " << speed << " km/h\n"; } }; int main() { Car myCar; // Accessing the public function of the base class myCar.setSpeed(60); // Accessing the public function of the derived class myCar.showSpeed(); // Accessing a function of the derived class that modifies the protected member of the base class myCar.accelerate(); myCar.showSpeed(); return 0; }
Setting speed to 60 km/h
Current speed of the car: 60 km/h
Accelerating! New speed: 70 km/h
Current speed of the car: 70 km/h


Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.