Introduction

In Object-Oriented Programming (OOP), access control refers to the mechanism that restricts the visibility and usage of class members (variables and methods) from outside the class. There are three types of controlling access function in OOPs-public, private and protected. This ensures that the internal state of the class is not directly manipulated by external code, providing better encapsulation and data security. Access control is essential for maintaining the integrity of the class and promoting good software design practices. Remember that access control is not a strict security feature but a design principle to guide developers in creating maintainable, secure, and encapsulated code. Some languages might provide additional access control modifiers or have slightly different rules for access control, but the general concepts remain consistent across most OOP languages. There are three types of controlling access function in OOPs-public, private and protected.

Defination

In C++, Object-Oriented Programming (OOP) allows you to control the access to class members (variables and functions) through access specifiers. C++ provides three access specifiers: public, private, and protected. These access specifiers determine how class members can be accessed from within the class itself and from outside the class.

Here’s an overview of each access specifier:

  1. Public:
    • Public members are accessible from anywhere in the program.
    • They can be accessed by objects of the class and any external functions or classes.
    • Public members represent the interface of the class, as they are accessible to the outside world and define how other parts of the program can interact with the class.
  2. Private:
    • Private members are accessible only within the class itself.
    • They cannot be accessed directly from outside the class or by derived classes.
    • Private members are used to implement the internal logic of the class and hide implementation details from external users.
  3. Protected:
    • Protected members are similar to private members, but they have an additional level of accessibility within derived classes.
    • They are accessible within the class and its derived classes but not from external functions or classes.

The access specifiers are used in the class definition, and they determine the access level of the subsequent class members until another access specifier is encountered. Here’s an example:

#include <iostream>

class MyClass {
public:
    int publicVar; // Public variable
    void publicFunction() { // Public function
        std::cout << "This is a public function." << std::endl;
    }

private:
    int privateVar; // Private variable
    void privateFunction() { // Private function
        std::cout << "This is a private function." << std::endl;
    }

protected:
    int protectedVar; // Protected variable
    void protectedFunction() { // Protected function
        std::cout << "This is a protected function." << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.publicVar = 42;         // Accessible
    obj.publicFunction();      // Accessible

    // obj.privateVar = 10;    // Not accessible, generates a compilation error
    // obj.privateFunction(); // Not accessible, generates a compilation error

    // obj.protectedVar = 20; // Not accessible, generates a compilation error
    // obj.protectedFunction(); // Not accessible, generates a compilation error

    return 0;
}

In this example, publicVar and publicFunction() are accessible from the main function, while privateVar, privateFunction(), protectedVar, and protectedFunction() are not accessible, and attempting to use them would result in compilation errors.

In this example, you can see how public, private, and protected access specifiers control the accessibility of class members. It’s essential to choose the appropriate access specifier to achieve proper encapsulation and control over class behavior. Public members provide the interface for users of the class, while private members hide the implementation details. Protected members facilitate inheritance and provide a controlled way to access base class functionality in derived classes.

It is essential to use access control to ensure data encapsulation and to enforce proper usage of class members within a C++ class. It’s worth mentioning that access control is an essential aspect of encapsulation in object-oriented programming, as it allows you to control how data and behaviors are accessed and modified, enhancing data security and code maintainability.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.