EZ

Eduzan

Learning Hub

Eduzan
Eduzan / C++

Encapsulation and Abstraction​

Encapsulation in C++ refers to the bundling of data and functions that operate on that data into a single unit or class. In Object-Oriented Programming (OOP), encapsulation plays a significant role in ensuring data protection by restricting direct access to an object’s attributes.

Real-Life Example of Encapsulation

Consider a company that has several departments, such as accounting, finance, and sales. Each department manages its own specific data:

  • The finance department handles all financial transactions and manages its own financial records.
  • The sales department is responsible for sales-related activities and keeps sales records.

If a finance department official needs to access sales data for a particular month, they cannot directly access it. Instead, they need to request the relevant data from someone in the sales department, maintaining controlled access to the information.

This exemplifies encapsulation: the sales department’s data and the methods to manage it are wrapped together, providing controlled access.

Key Properties of Encapsulation

1. Data Protection: Encapsulation safeguards an object’s internal state by keeping its data members private. External code can only interact with the data through public methods, which ensures that the data is manipulated securely and in a controlled manner.

2. Information Hiding: Encapsulation conceals the internal workings of a class, exposing only a public interface to the outside world. This abstraction simplifies the use of the class, as external code doesn’t need to understand the internal details, allowing those details to be changed without impacting other parts of the program.

Example 1: Simple Encapsulation

#include <iostream>
using namespace std;

class Number {
private:
    int value;

public:
    void setValue(int input) {
        value = input;
    }

    int getHalf() {
        return value / 2;
    }
};

int main() {
    Number num;
    int n;
    cin >> n;
    num.setValue(n);
    cout << "Half of " << n << " is: " << num.getHalf() << endl;
    return 0;
}

In this example, the class Number encapsulates the data (the integer value) and provides a method to manipulate that data (calculate half of the number). The value member is private and can only be accessed or modified through the class’s public methods.

Features of Encapsulation

  • Functions inside the class must interact with the class’s member variables. This ensures that encapsulation is effective.
  • Encapsulation improves the readability, maintainability, and security of code by organizing related data and methods into a cohesive unit.
  • It also provides control over how data members are modified, promoting better data integrity.

Example 2: Person Class

#include <iostream>
#include <string>
using namespace std;

class Person {
private:
    string name;
    int age;

public:
    Person(string n, int a) : name(n), age(a) {}

    void setName(string n) {
        name = n;
    }

    string getName() {
        return name;
    }

    void setAge(int a) {
        age = a;
    }

    int getAge() {
        return age;
    }
};

int main() {
    Person person("Alice", 25);

    cout << "Name: " << person.getName() << endl;
    cout << "Age: " << person.getAge() << endl;

    person.setName("Bob");
    person.setAge(30);

    cout << "Name: " << person.getName() << endl;
    cout << "Age: " << person.getAge() << endl;

    return 0;
}
End of lesson.