C++ Access Specifiers

C++ access specifiers are keywords used to specify the accessibility or visibility of class members (variables and functions) and class itself. There are three types of access specifiers in C++: public, protected, and private.

Public

The public access specifier is the most permissive access level. Members and functions declared as public can be accessed from anywhere, both inside and outside the class. This means that any function or variable declared as public can be accessed by any other function or variable, regardless of where it is located in the program.

For example, consider the following class:

class MyClass {
public:
    int myInt;
    void myFunction() {
        cout << "This is a public function" << endl;
    }
};

int main() {
    MyClass myObject;
    myObject.myInt = 5;
    myObject.myFunction();
    return 0;
}

In this example, the myInt variable and myFunction() function are both declared as public. This means that they can be accessed and modified from outside the class, as shown in the main() function. The output of this program would be:

This is a public function

Protected

The protected access specifier is less permissive than public, but more permissive than private. Members and functions declared as protected can be accessed from within the class and from derived classes, but not from outside the class. This means that any function or variable declared as protected can be accessed by any other function or variable that is located in the same class or in a derived class, but not by any function or variable located outside the class.

For example, consider the following class:

class MyBase {
protected:
    int myInt;
    void myFunction() {
        cout << "This is a protected function" << endl;
    }
};

class MyDerived: public MyBase {
public:
    void myDerivedFunction() {
        myInt = 5;
        myFunction();
    }
};

int main() {
    MyDerived myObject;
    myObject.myDerivedFunction();
    return 0;
}

In this example, the myInt variable and myFunction() function are both declared as protected in the MyBase class. This means that they can be accessed and modified from within the MyBase class and from the derived class MyDerived, but not from outside the class. As a result, the myDerivedFunction() function can access and modify the myInt variable and call the myFunction() function, but the main() function cannot. The output of this program would be:

This is a protected function

Private

The private access specifier is the most restrictive access level. Members and functions declared as private can only be accessed from within the class. This means that any function or variable declared as private can only be accessed by other functions or variables that are located in the same class, and not by any function or variable located outside the class or in a derived class. This provides the highest level of encapsulation and data hiding, as private members and functions cannot be accessed or modified from outside the class.

For example, consider the following class:

class MyClass {
private:
    int myInt;
    void myFunction() {
        cout << "This is a private function" << endl;
    }
};
int main() {
MyClass myObject;
myObject.myInt = 5; // This will result in a compile-time error
myObject.myFunction(); // This will also result in a compile-time error
return 0;
}

In this example, the myInt variable and myFunction() function are both declared as private in the MyClass class. This means that they can only be accessed and modified from within the MyClass class, and not from outside the class or in a derived class. As a result, the main() function cannot access or modify the myInt variable or call the myFunction() function, and this will result in a compile-time error. The output of this program would not be generated.

Access Specifiers and Inheritance

In C++, when a class is inherited by another class, the access specifiers of the members and functions of the base class affect the accessibility of those members and functions in the derived class. Public members and functions of the base class are accessible as public members and functions in the derived class, protected members and functions are accessible as protected members and functions, and private members and functions are not accessible in the derived class at all.

It is important to note that the access level of a member or function in a derived class cannot be more permissive than its access level in the base class. For example, if a member or function is declared as private in the base class, it cannot be accessed as public or protected in the derived class. On the other hand, if a member or function is declared as public in the base class, it can also be accessed as public in the derived class.

Conclusion

In conclusion, C++ access specifiers are an important aspect of the language, as they provide a way to control the accessibility and visibility of class members and functions. The three types of access specifiers in C++, public, protected, and private, each have their own level of permissiveness and provide different levels of encapsulation and data hiding. Understanding and using access specifiers correctly is essential for creating robust, maintainable, and secure C++ code.

Leave a Reply

Your email address will not be published. Required fields are marked *