Access Specifiers & Encapsulation

🟡 Intermediate

📖 Definition

Encapsulation bundles data and methods together while hiding implementation details behind private access specifiers, exposing controlled validation interfaces via public getters and setters.

💡 Code Example

#include <iostream>

class BankAccount {
private:
    double balance{0.0};

public:
    void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    double getBalance() const { return balance; }
};

int main() {
    BankAccount acc;
    acc.deposit(500.0);
    std::cout << "Balance: $" << acc.getBalance() << "\n";
    return 0;
}