C++

Access Specifiers

Access specifiers control how the members (attributes and methods) of a class can be accessed.

They help protect data and organize code so that only the right parts can be seen or changed.

We have already used the public access specifier in our previous class lessons.

Type of access specifiers

There are three types of access specifiers.

  1. Public: members are accessible from outside the class
  2. Private: members cannot be accessed (or viewed) from outside the class
  3. Protected members cannot be accessed from outside the class, however, they can be accessed in inherited classes. We will later learn about inheritance in our next lesson.

By default, C++ members are private if no access specifier specified.

We've already seen numerous examples of the public access specifier so we will have sample code to demonstrate the private.

We will also demonstrate the proctected during inheritance lesson.

#include <iostream>
using namespace std;
class AccessClass{
    int a = 20;//private access by default
    int b = 5;//private access by default
    private:
    int x = 5;
    int y = 10;
    void welcome(){
        cout << "Welcome To C++ Programming" << endl;
    }
    public:
    int i;
    int z;
};
int main() {
    AccessClass a;
    //Assigning values to private attributes (variables) produces error
    a.a = 7;
    a.b = 3;
    a.x = 1;
    a.y = 10;
    //Showing the values of private attributes (variables) produces error
    cout << "a = " << a.a << " b = " << a.b << " x = " << a.x << " y = " << a.y << endl;
    a.welcome();
    //Public attributes can be assigned values and accessed
    a.i = 20;
    a.z = 15;
    cout << "i = " << a.i << " z = " << a.z << endl;

    return 0;
}

Notes:

  1. When you run the above code, you will get error at the lines where you are trying to assign value to a private variable or access a private variable.
  2. A method could also be specified as private and can only be used in the class and not outside
  3. You must comment those lines in order to test the other parts of the code
  4. As you may have observed, only the assigning and accessing the public variables are allowed

Real-Life Example

  1. Public: Like the front door of your house - anyone can come in.
  2. Private: Like a locked drawer - only the owner (or trusted friends) can open it.
  3. Protected: Like a family-only room - children (subclasses) can enter, others cannot.

Advantages of Access Specifiers

  1. Enables Encapsulation: Combines data and behavior seamlessly. It protects the internal state of your objects.
  2. Facilitates Data Hiding: Keeps critical implementation details completely hidden. It exposes only the absolute necessary interface.
  3. Guarantees Data Integrity: Prevents outside code from corrupting variable values. Forces all state changes through validation methods.
  4. Simplifies Code Maintenance: Allows internal class logic changes easily. These internal shifts will not break external code.
  5. Supports Safe Inheritance: Employs the protected specifier for derived classes. It opens access for child classes while blocking outsiders.
  6. Reduces Programming Errors: Catches unauthorized direct manipulation at compile time. It actively saves developers from accidental code tampering.