Access Specifiers in C++.

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.
Similarly, in the programming world, there are three access specifiers: 
  • public. 
  • private.
  • protected.

Let's understand each specifier one by one in detail:

1. Public: Members declared as public are accessible from any part of the program. They have no restrictions on access. Data members and Member Functions which is declared as public can be accessible by different class or function as well. They form the interface of the class, and users can interact with these members freely.

Example Code:

// 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;
}
Output:
Breadth: 5
Length: 10
Aread of Rectangle: 50

In the above example, the data member length and breadth are declared as public so we can access and modify their value outside the class.

2. Private: Members declared as private are only accessible within the same class. They are not accessible from outside the class. They are used to encapsulate the internal implementation details of the class.

Example Code:
// 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;
}
Output:
output screenshot for private access specifier

In this above example, the data members and member function are declared as private so we cannot access them outside the class and get the above error.

3. Protected: Members declared as protected are similar to private members but have one additional feature: they can be accessed in the derived classes. They are not accessible from outside the class. They allow derived classes to access certain members while still restricting access to the external world.

Example Code:
//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;
}
Output:
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

In this example, the Vehicle is the base class, and it has a protected member speed. Car is a derived class from Vehicle. The derived class Car can access and modify the protected member speed of the base class Vehicle.

So I hope you understand the working and use of Access specifiers and their contribution to the principles of encapsulation and data hiding in object-oriented programming.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS