Data Encapsulation is one of the fundamental concepts of Object-Oriented Programming (OOP). It refers to the bundling of data (attributes) and the methods (functions) that operate on that data into a single unit, known as a class. In addition, encapsulation involves restricting direct access to some of an object's components and only allowing controlled access via public methods, commonly known as getters and setters.
The primary goal of encapsulation is to protect the internal state of an object from unwanted changes and to hide implementation details from the outside world. This helps to ensure that the object’s data is manipulated only in well-defined and controlled ways.
Private Data Members:
Public Getter and Setter Methods:
Control Over Data:
Information Hiding:
Data Integrity:
Modularity:
Security:
Reduced Complexity:
Code Reusability and Maintenance:
Let’s see an example of how data encapsulation works in C++:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Private data member
public:
// Constructor to initialize balance
BankAccount(double initial_balance) {
if (initial_balance > 0) {
balance = initial_balance;
} else {
balance = 0; // If invalid balance is provided, set it to 0
}
}
// Getter method to access the balance
double getBalance() {
return balance;
}
// Setter method to deposit money (ensures positive deposits)
void deposit(double amount) {
if (amount > 0) {
balance += amount;
} else {
cout << "Deposit amount must be positive!" << endl;
}
}
// Setter method to withdraw money (ensures no overdraft)
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
cout << "Invalid withdrawal amount!" << endl;
}
}
};
int main() {
BankAccount account(1000); // Create a bank account object with an initial balance of 1000
// Access and manipulate the balance via getter and setter methods
cout << "Initial Balance: $" << account.getBalance() << endl;
account.deposit(500); // Deposit $500
cout << "After Deposit: $" << account.getBalance() << endl;
account.withdraw(200); // Withdraw $200
cout << "After Withdrawal: $" << account.getBalance() << endl;
account.withdraw(2000); // Try to withdraw an amount greater than the balance
cout << "After Attempted Overdraft: $" << account.getBalance() << endl;
return 0;
}
Private Data Member:
balance is a private data member of the BankAccount class, so it cannot be accessed directly from outside the class.Getter Method:
getBalance() is a public method that allows access to the value of the balance. This is the only way to retrieve the balance from outside the class.Setter Methods:
deposit(double amount) and withdraw(double amount) are public methods that allow modification of the balance. However, these methods include logic to ensure that only valid operations are performed (e.g., no negative deposits and no overdrafts).Encapsulation in Action:
BankAccount object only through its public methods (getBalance(), deposit(), withdraw()). The internal state (balance) is protected from direct access, ensuring the integrity of the object's data.A real-world analogy of encapsulation is like a TV remote control:
Encapsulation is a core principle of OOP, making it easier to manage complex systems by isolating object details and exposing only necessary interfaces. It enables safer and more maintainable code by controlling how data is accessed and modified.
Open this section to load past papers