Encapsulation
Encapsulation is a fundamental pillar of object-
oriented programming (OOP). It's a powerful tool for
structuring and organizing code, improving
maintainability, and protecting data integrity.
>> It's a fundamental concept of object-oriented
programming (OOP) that bundles data and the
methods that operate on that data within a single
unit, known as a class.
>> This allows for better organization, control over
access, and increased flexibility in code design.
CODE
#include <iostream>
using namespace std;
class BankAccount {
OUTPU
T
private:
int accountNumber;
double balance;
public:
Account Number: 123
BankAccount(int accNum, double bal) : accountNumber(accNum), balance(bal) {}
int getAccountNumber() { return accountNumber; } Initial Balance: 1000
double getBalance() { return balance; } Updated Balance: 150
void setBalance(double bal) { balance = bal; }
};
int main() {
BankAccount account(12345, 1000.0);
cout << "Account Number: " << account.getAccountNumber() << endl;
cout << "Initial Balance: " << account.getBalance() << endl;
account.setBalance(1500.0);
cout << "Updated Balance: " << account.getBalance() << endl;
return 0;
}