Understanding Access Modifiers in C++

Understanding Access Modifiers in C++

What Are Access Modifiers?

In C++, access modifiers are keywords used to define the level of access that members of a class (variables and methods) can have. They are essential for ensuring encapsulation, which is a core concept of Object-Oriented Programming (OOP). By using access modifiers, we control how the internal data of a class is accessed, modified, and protected.

Access modifiers in C++ include:

  • private: Members are accessible only within the same class.
  • protected: Members are accessible within the class and derived classes.
  • public: Members are accessible from anywhere in the program.

By controlling access, access modifiers help:

  1. Protect the internal state of a class.
  2. Promote data encapsulation.
  3. Allow controlled access to data and functionality.

Types of Access Modifiers

  1. private:
    • Definition: Members marked as private are accessible only within the same class. They are hidden from derived classes and external code.
    • Use Case: To protect internal data and ensure that class users can’t modify sensitive data directly.
  2. protected:
    • Definition: Members marked as protected are accessible within the same class and any derived (child) classes. However, they are not accessible by objects of the class.
    • Use Case: To allow derived classes to access and modify base class data, while still hiding the data from the outside world.
  3. public:
    • Definition: Members marked as public are accessible from anywhere. This includes external code, objects, and even derived classes.
    • Use Case: For functions and variables that need to be accessed or modified by external code.

Code Example

class number {
private:
int privateVar; // Private variable, accessible only within this class.

protected:
int protectedVar; // Protected variable, accessible within the class and derived classes.

public:
int publicVar; // Public variable, accessible from anywhere.

// Constructor to initialize the variables
number() {
privateVar = 10;
protectedVar = 20;
publicVar = 30;
}

// Public function to display values of all members
void display() {
cout << "Private Variable: " << privateVar << endl;
cout << "Protected Variable: " << protectedVar << endl;
cout << "Public Variable: " << publicVar << endl;
}
};

Explanation of the Base Class

  1. Private Member (privateVar):
    • The privateVar is declared private, meaning it can only be accessed inside the number class. It is hidden from both derived classes and external code.
    • Purpose: Used to encapsulate and protect data that shouldn’t be directly modified by the outside world.
  2. Protected Member (protectedVar):
    • The protectedVar is declared protected, meaning it can be accessed within the number class and any class derived from it (like the Derived class).
    • Purpose: Useful for allowing derived classes to interact with base class members, while still hiding the data from external code.
  3. Public Member (publicVar):
    • The publicVar is declared public, meaning it is accessible from anywhere in the program, including objects of the class, and even in derived classes.
    • Purpose: Typically used for properties that need to be freely accessed or modified by external code.
  4. Constructor:
    • The constructor initializes all three variables with values (privateVar = 10, protectedVar = 20, publicVar = 30).
  5. Function (display):
    • The display() function prints the values of the three variables to show how access modifiers impact the visibility of the class members.

Derived Class Code (Derived class)

class Derived : public number {
public:
// Accessing protected variable in the derived class
void displayProtected() {
cout << "Protected Variable (from Derived class): " << protectedVar << endl;
}
};

Explanation of the Derived Class

  1. Inheriting from number:
    • The Derived class inherits from the number class using public inheritance. This means all public and protected members of the number class are inherited by Derived.
  2. Accessing Protected Member:
    • The displayProtected() function in the Derived class prints the value of protectedVar. This is allowed because protectedVar is protected and can be accessed by derived classes.
  3. Private Members Not Inherited:
    • privateVar is a private member of the number class and cannot be accessed directly by the Derived class. This demonstrates the limited access of private members.

Main Function Code

int main() {
// Object of base class
number obj;

// Accessing public variable directly
cout << "Public Variable: " << obj.publicVar << endl;

// Displaying all variables using the public function
obj.display();

// Object of derived class
Derived derivedObj;

// Accessing protected variable via derived class
derivedObj.displayProtected();

return 0;
}

Explanation of the Main Function

  1. Creating obj (Object of number):
    • obj is an instance of the number class. Using this object, we can access the public member publicVar directly.
  2. Calling display():
    • The obj.display() function is called, which prints the values of all three variables (private, protected, and public). Although privateVar is private, it can still be accessed within the class through a public function.
  3. Creating derivedObj (Object of Derived):
    • derivedObj is an instance of the Derived class. Since Derived inherits from number, it can access the protected member protectedVar through the displayProtected() function.

Output of the Code

Public Variable: 30
Private Variable: 10
Protected Variable: 20
Public Variable: 30
Protected Variable (from Derived class): 20

Explanation of the Output

  1. Public Variable: 30:
    • This value is accessed directly from the obj object since it is a public member.
  2. Private Variable: 10:
    • This value is printed by the display() function. Although it is private, it can be accessed within the class.
  3. Protected Variable: 20:
    • This value is printed by the display() function. protectedVar is accessible within the number class.
  4. Protected Variable (from Derived class): 20:
    • This value is printed by the displayProtected() function in the Derived class. The derived class can access protected members of the base class.

Access Summary Table

Access ModifierAccess Inside ClassAccess in Derived ClassAccess via Object
private
protected
public

Conclusion

In this tutorial, we explored the three main access modifiers in C++:

  • private: Restricts access to within the class.
  • protected: Allows access within the class and derived classes, but not by external code.
  • public: Grants access to members from anywhere in the program.

By using these access modifiers, we can control data visibility, ensuring that class members are accessible in a controlled and secure manner. Access modifiers are fundamental in achieving encapsulation and data hiding, which are essential principles of object-oriented programming.

Leave a Reply

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