Encapsulation in C++

Encapsulation in C++

The wrapping up of data and information into a single unit is referred to as encapsulation in the C++ language. Encapsulation is the bringing together of the data and the functions that manipulate them in object-oriented programming.

Consider a practical illustration of encapsulation: at a firm, there are several divisions, such as the accounts division, the finance division, the sales division, etc. Now,

The finance department manages all financial transactions and maintains records of all financial data. In a similar vein, the sales division manages all actions linked to sales and maintains track of all sales.

Now a scenario could occur where a finance official needs all the sales information for a specific month for some reason. He is not permitted to view the sales section's data directly in this instance. To seek the specific information, he must first speak with another officer in the sales department.

Encapsulation is what it is. Here, the term "sales section" refers to both the data of the sales section and the employees who can manipulate them.

Features of Encapsulation :

The qualities of encapsulation are listed below:

  1. No function from the class may be accessed directly. To access the method that uses the class member variables, we require an object.

  2. The function which we are making inside the class must use only member variables, only then it is called encapsulation..

  3. If we don’t make a function inside the class which is using the member variable of the class then we don’t call it encapsulation.

  4. Increase in the security of data

  5. It helps to control the modification of our data members.

In C++, encapsulation is implemented using access specifiers - public, private, and protected - which control the visibility of class members from outside the class.

public members are accessible from outside the class and define the interface that the class provides to its users. These are the methods and variables that are meant to be used by the clients of the class. For example, the public methods of a class might allow the client to set or get values of member variables, or perform some operations on them.

private members, on the other hand, are hidden from the outside world and are only accessible from within the class. These are the implementation details of the class and should not be exposed to the clients. Private members are typically used to store the data that the class needs to maintain its state, or to implement the behavior of the public methods.

protected members are similar to private members, but they are accessible to the derived classes. These members are used when we want to provide some degree of access to the base class's implementation details to the derived classes.

The main benefits of encapsulation are:

  1. Information hiding: By hiding the implementation details of a class, we can protect the internal state of the class from being accidentally or intentionally modified by the clients, which could lead to inconsistent or invalid states.

  2. Abstraction: Encapsulation allows us to provide a high-level abstraction of the class to the clients, without revealing the implementation details. This makes it easier for the clients to use the class without worrying about the internal details.

  3. Modularity: Encapsulation allows us to change the implementation of a class without affecting the clients, as long as the public interface remains the same. This makes the code more modular and easier to maintain.

Here is an example of encapsulation in C++:

cCopy codeclass BankAccount {
private:
    int accountNumber;
    double balance;
public:
    BankAccount(int accNo, double bal) {
        accountNumber = accNo;
        balance = bal;
    }
    void deposit(double amount) {
        balance += amount;
    }
    void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
        } else {
            cout << "Insufficient funds!" << endl;
        }
    }
    double getBalance() const {
        return balance;
    }
};

int main() {
    BankAccount account(12345, 1000.0);
    account.deposit(500.0);
    account.withdraw(200.0);
    cout << "Balance: " << account.getBalance() << endl;
    return 0;
}

In this example, we define a BankAccount class with private member variables accountNumber and balance, and public methods deposit(), withdraw(), and getBalance() that provide a public interface to access and manipulate the account balance. The implementation details of the class, such as the account number and the balance, are hidden from the clients. The clients can only access the public interface of the class to perform operations on the account. This ensures that the internal state of the account is not modified directly by the clients, and that the account maintains its integrity.

We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.