C++ Access modifiers

In C++, access modifiers are keywords used to specify the visibility and accessibility of members (variables and methods) of a class. They control how and where the members can be accessed from within the program. There are three primary access modifiers in C++.

1. public

Members declared as public are accessible from anywhere in the program (both inside and outside the class).

Note: public members can be accessed directly through an object of the class.

Example:


#include <iostream>
#include <string> // For using the string type
using namespace std;

class Employee {
public:
    string name;

    void getEmployeeName() {
        cout << "Name: " << name << endl;
    }
};

int main() {
    // Creating objects (instances of the Employee class)
    Employee emp1;
    emp1.name = "John";  // Assigning value to the name member
    emp1.getEmployeeName();  // Calling the member function to print the name
    
    return 0;
}

Explanation:

  • The name field is declared public, meaning they can be accessed directly from outside the class (in the Main method).
  • The method getEmployeeName is a public, so it can be called from any other code, not just within the Employee class.

Output:

Name: John

2. private

Members declared as private are not accessible from outside the class. They can only be accessed or modified by the member functions of the class (or friends of the class).

Example:


#include <iostream>
#include <string> // For using the string type
using namespace std;

class Employee {
private:
    string name;

public:
    // Constructor to initialize the name
    Employee(string empName) {
        name = empName;
    }

    // Method to print the employee's name
    void getEmployeeName() {
        cout << "Name: " << name << endl;
    }
};

int main() {
    // Creating objects (instances of the Employee class)
    Employee emp1("John");

    // Calling method to display the employee's name
    emp1.getEmployeeName();

    return 0;
}

Explanation:

  1. The name field is private, meaning it cannot be accessed directly from outside the class.
  2. It can only be accessed within the class itself (for example, in the getEmployeeName method).

3. protected

In C++, the protected access modifier is used to restrict access to members (variables and methods) of a class. A member marked as protected can be accessed within the class itself and by derived (sub) classes. However, it cannot be accessed from outside the class unless through a public or protected method.

Example:


#include <iostream>
#include <string> // For using the string type
using namespace std;

class Employee {
protected:
    string name;  // Protected member variable

public:
    // Constructor to initialize the name
    Employee(string empName) {
        name = empName;
    }

    // Public method to access the protected name
    void getEmployeeName() {
        cout << "Name: " << name << endl;
    }
};

class Manager : public Employee {
public:
    // Constructor calling the base class constructor
    Manager(string empName) : Employee(empName) {}

    // Method to access the protected member of the base class
    void showEmployeeName() {
        cout << "Manager's Name: " << name << endl;
    }
};

int main() {
    Employee emp1("John");
    emp1.getEmployeeName();  // Allowed: public method can access protected member

    Manager mgr("Matt");
    mgr.showEmployeeName();  // Allowed: derived class can access protected member

    // emp1.name = "NewName";  // ERROR: Cannot access 'name' directly as it's protected

    return 0;
}

Explanation:

1. Employee class: The name member is marked as protected, so it's accessible within the class and its derived classes.

2. Manager class: Inherits from Employee and can directly access name because it’s protected. It has its own method showEmployeeName() that prints the name.

3. Main function: You cannot access the name directly from outside the class (like trying emp1.name = "NewName"), but you can use public methods like getEmployeeName() to access it.

Output:

Name: John
Manager's Name: Matt