C++ Access Specifiers

What are Access Specifiers?

In C++ access specifiers are keywords that decide how easily class elements, like data members and member functions can be reached. They manage which sections of a class are open, to parts of the program defining the degree of encapsulation and visibility.

C++ provides three types of access specifiers:

  • Public
  • Private
  • Protected

Types of Access Specifiers

Public

Public members can be reached from any part of the program outside the class and its subclasses.

Private

Private members are accessible only within the class they are declared in. They cannot be accessed by derived classes or other parts of the program, ensuring data hiding and encapsulation.

Protected

Protected members are like members but can also be accessed by classes that inherit from them. They are kept hidden from the rest of the program. Can be utilized in inheritance.

When a class is inherited the access specifier determines how the derived class interacts, with the base class members.

Example of Access Specifiers

class MyClass {
public:
    int publicVar;      // Public member

protected:
    int protectedVar;   // Protected member

private:
    int privateVar;     // Private member
};

int main() {
    MyClass obj;
    obj.publicVar = 10; // Accessible and modifiable
    // obj.protectedVar = 20; --> Inaccessible and not modifiable directly
    // obj.privateVar = 30; --> Inaccessible and not modifiable
    return 0;
}

In this case publicVar can be accessed from any location. Privatevar is limited to use within the class. On the hand protectedVar can be accessed within the class and any classes derived from it.

Importance of Access Specifiers in OOP

Access specifiers are crucial in object-oriented programming (OOP) as they control the visibility and accessibility of class members, supporting encapsulation and inheritance.

  • Encapsulation
    Encapsulation hides the internal implementation details of a class from outside access. Access specifiers like private protect the internal state of an object, ensuring data security and consistency.
  • Inheritance
    Inheritance allows new classes to inherit properties from existing ones. Access specifiers like protected enable derived classes to access certain members of the base class, promoting code reuse.

Public Access Specifier

Definition and Usage

The public access modifier enables class elements to be accessed from, in the program. It comes in handy when designing interfaces that can be readily used by classes or sections of code.

Example

class Rectangle {
public:
    int length;
    int width;

    // Public member function to calculate area
    int calculateArea() {
        return length * width;
    }
};

int main() {
    Rectangle myRectangle;
    myRectangle.length = 5;
    myRectangle.width = 3;
    int area = myRectangle.calculateArea();
    std::cout << "Area: " << area << std::endl;
    return 0;
}

In this instance the measurements of length and width are considered attributes that are accessible and alterable directly. Additionally the calculateArea() method is accessible, to the public. Can be invoked from external sources.

Advantages of Using Public Functions

  • Enhanced Accessibility; Public functions are easily accessible from any section of the program simplifying interaction with the class.
  • Improved Code Reusability; Public functions can be utilized across sections of the program minimizing the necessity for repeating code.
  • Secured Encapsulation; Public functions offer regulated access, to a class state ensuring that implementation details are kept confidential and safeguarded.

Private Access Specifier

The private access specifier limits the access to class members making them accessible, within the class. This helps in keeping data and functions secure and safeguarded from unauthorized changes or access.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate