Encapsulation in c++

Encapsulation in c++

Encapsulation is one of the fundamental concepts of object-oriented programming, and it refers to the practice of hiding implementation details of a class from its users and providing a public interface to access and manipulate the data and behavior of the class.

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.