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


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

Subject: Object Oriented Programming

Encapsulation and Data Abstraction

Encapsulation is the process of bundling data and methods that operate on that data into a single unit, called an object. Data abstraction is the process of defining the essential characteristics of an object without specifying its implementation details. Encapsulation and data abstraction are two fundamental concepts in object-oriented programming (OOP) that help to improve the security, maintainability, and reusability of software applications.

Encapsulation

Encapsulation allows us to hide the implementation details of an object from other parts of the program. This can make it easier to maintain the program, as we can change the implementation of an object without affecting the rest of the program. Encapsulation also helps to improve the security of a program, as it can prevent unauthorized access to data and methods.

Data Abstraction

Data abstraction allows us to define the essential characteristics of an object without specifying its implementation details. This makes it possible to create objects that can be used in different programs without having to rewrite the code for each program. Data abstraction also helps to improve the maintainability of a program, as we can change the implementation of an object without affecting the rest of the program.

C++ Code Example

The following C++ code shows how to achieve encapsulation and data abstraction in a simple program.

#include 

using namespace std;

class Employee {
private:
    int employeeID;
    string name;
    float salary;
public:
    void setEmployeeID(int id) {
        employeeID = id;
    }
    int getEmployeeID() {
        return employeeID;
    }
    void setName(string n) {
        name = n;
    }
    string getName() {
        return name;
    }
    void setSalary(float s) {
        salary = s;
    }
    float getSalary() {
        return salary;
    }
};

int main() {
    Employee emp;
    emp.setEmployeeID(101);
    emp.setName("John Doe");
    emp.setSalary(50000);

    cout << "Employee ID: " << emp.getEmployeeID() << endl;
    cout << "Employee Name: " << emp.getName() << endl;
    cout << "Employee Salary: " << emp.getSalary() << endl;

    return 0;
}

In this example, the Employee class has three private data members: employeeID, name, and salary. These data members are only accessible within the Employee class.

The class also has six public member functions: setEmployeeID(), getEmployeeID(), setName(), getName(), setSalary(), and getSalary().

These functions allow us to access and modify the private data members of the class.

The main() function creates an instance of the Employee class named emp and uses the public member functions to set and get the values of the private data members.

Encapsulation and data abstraction are fundamental concepts in OOP that help to improve the security, maintainability, and reusability of software applications.