What are access modifiers? How many types of access modifiers are there in C++? Explain each of them in detail and also give an example of implementing the data hiding in object-oriented programming.


Q.) What are access modifiers? How many types of access modifiers are there in C++? Explain each of them in detail and also give an example of implementing the data hiding in object-oriented programming.

Subject: Object Oriented Programming and Methodology

Access Modifiers

Access modifiers control the accessibility of a class's members (variables and functions) to objects of other classes. In C++, there are four types of access modifiers:

  1. Public: Members declared as public can be accessed from anywhere within the program. This means that any function can access the public members of any object of the class.

Example:

class MyClass {
public:
    int x;
    void printX() {
        std::cout << "x = " << x << std::endl;
    }
};

int main() {
    MyClass object;
    object.x = 10;
    object.printX();
}

Output:

x = 10
  1. Protected: Members declared as protected can only be accessed from within the class itself and from classes that inherit from that class. This means that a function in a derived class can access the protected members of the base class.

Example:

class Base {
protected:
    int x;
    void printX() {
        std::cout << "x = " << x << std::endl;
    }
};

class Derived : public Base {
public:
    void accessProtectedMember() {
        x = 10;  // Can access protected member of the base class
        printX();
    }
};

int main() {
    Derived object;
    object.accessProtectedMember();
}

Output:

x = 10
  1. Private: Members declared as private can only be accessed from within the class itself. This means that no function outside of the class can access the private members of the class.

Example:

class MyClass {
private:
    int x;
    void printX() {
        std::cout << "x = " << x << std::endl;
    }
};

int main() {
    MyClass object;
    object.x = 10;  // Error: cannot access private member
    object.printX(); // Error: cannot access private member
}
  1. Default: If no access modifier is specified, the default access modifier is applied. The default access modifier is private for members of a class and public for members of a struct.

Data Hiding in Object-Oriented Programming

Data hiding is a technique used to restrict access to an object's members. This is achieved by making the data members of a class private and providing public methods to manipulate the data. This way, the implementation details of the class are hidden from the user, and the user can only interact with the class through the public methods.

For example, consider the following class:

class Person {
private:
    std::string name;
    int age;

public:
    void setName(std::string name) {
        this->name = name;
    }

    std::string getName() {
        return this->name;
    }

    void setAge(int age) {
        this->age = age;
    }

    int getAge() {
        return this->age;
    }
};

In this example, the data members name and age are declared as private, which means that they cannot be accessed directly from outside the class. Instead, the user must use the public methods setName(), getName(), setAge(), and getAge() to manipulate the data. This way, the implementation details of the class are hidden from the user, and the user can only interact with the class through the public methods.