Explain the concept of Encapsulation and Data abstraction in detail. Write a C++ code to show these concepts.


Q.) Explain the concept of Encapsulation and Data abstraction in detail. Write a C++ code to show these concepts.

Subject: Object Oriented Programming

Encapsulation:

Encapsulation is a fundamental concept in object-oriented programming that combines data and the methods that operate on that data into a single unit, known as an object. The goal of encapsulation is to bundle data and behavior together, allowing developers to access and modify data only through the object's methods. This approach enhances code security and promotes data integrity by limiting direct access to the object's internal state.

Data Abstraction:

Data abstraction is closely related to encapsulation. It refers to the act of exposing only essential properties and behaviors of an object while hiding the underlying implementation details. Data abstraction allows developers to create simplified conceptual models of objects, focusing on their functionality and behavior rather than their internal structure. By abstracting data, developers can create more flexible and maintainable code, as changes to the underlying implementation can be made without affecting the client code.

C++ Code Example:

class Person {
private:
    string name;
    int age;

public:
    // Constructor
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    // Getter methods (accessors)
    string getName() {
        return name;
    }

    int getAge() {
        return age;
    }

    // Setter methods (mutators)
    void setName(string name) {
        this->name = name;
    }

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

int main() {
    // Create a Person object
    Person person("John Doe", 30);

    // Access the object's attributes through getter methods
    cout << "Name: " << person.getName() << endl;
    cout << "Age: " << person.getAge() << endl;

    // Modify the object's attributes through setter methods
    person.setName("Jane Doe");
    person.setAge(35);

    // Print the updated values
    cout << "Updated Name: " << person.getName() << endl;
    cout << "Updated Age: " << person.getAge() << endl;

    return 0;
}

Explanation:

In this C++ code, we define a Person class with private data members name and age. These private data members ensure encapsulation by restricting direct access to the object's internal state.

We provide public getter and setter methods to access and modify these private data members. The getter methods allow us to retrieve the values of name and age, while the setter methods allow us to update these values.

In the main function, we create an instance of the Person class, set its initial values, and then use the getter and setter methods to access and modify the object's attributes. This demonstrates how encapsulation and data abstraction allow us to control access to and maintain the integrity of the object's internal state.