Encapsulation is a fundamental concept in object-oriented programming that involves bundling data and methods together within a class, and controlling access to that data and methods. Information hiding is a technique used in encapsulation that involves hiding the implementation details of the class from the outside world, and only exposing a public interface for interacting with the class. C++ supports encapsulation and information hiding through the use of access specifiers. Access specifiers in C++ include `public`, `private`, and `protected`, and they control the visibility of class members. `public` members can be accessed from outside the class, `private` members can only be accessed from within the class, and `protected` members can be accessed by derived classes. By default, class members are `private`. Encapsulation and information hiding provide several benefits, including: 1. Encapsulation ensures that the implementation details of a class are hidden from the outside world. This makes it easier to modify the implementation of the class without affecting code that uses the class. 2. Encapsulation allows for better control over the way in which data and methods are accessed. By controlling access to class members, you can ensure that they are used correctly and prevent unintended modifications. 3. Encapsulation promotes code reuse and modularity. By bundling data and methods together within a class, you can create a reusable component that can be used in multiple parts of your code. Here's an example of how encapsulation and information hiding can be used in C++: ``class BankAccount { private: string accountNumber; double balance; public: void deposit(double amount) { balance += amount; } void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { cout << "Insufficient funds." << endl; } } double getBalance() { return balance; } }; int main() { BankAccount account1; account1.deposit(1000); account1.withdraw(500); cout << "Balance: " << account1.getBalance() << endl; return 0; }
In this example, a `BankAccount` class is defined that contains two data members: an `accountNumber` and a `balance`. The `accountNumber` data member is declared as `private`, which means it can only be accessed from within the class. The `balance` data member is declared as `private`, which means it can only be accessed from within the class. Two member functions `deposit()` and `withdraw()` are provided to modify the `balance` data member, while a `getBalance()` member function is provided to retrieve the `balance` data member. These member functions are declared as `public`, which means they can be accessed from outside the class.
By encapsulating the `BankAccount` class and hiding the implementation details of the class (the `accountNumber` data member), the class canbe used safely and efficiently. The data members of the `BankAccount` class are accessed and modified only through the member functions, which ensures that they are used correctly and prevents unintended modifications. This promotes code reuse and modularity, as the `BankAccount` class can be used in multiple parts of the code without worrying about the implementation details.