// Example program demonstrating C++ encapsulation feature
// Core idea: Make data (member variables) private, only access and modify through public member functions,
// thus hiding implementation details and ensuring data security and integrity

#include <iostream>

using namespace std;

class BankAccount {
private:
    double balance_;  // Private member variable: balance (cannot be accessed directly from outside)

public:
    // Constructor: initialize account, ensure initial balance is valid (non-negative)
    BankAccount(double initial_balance = 0.0) {
        if (initial_balance < 0) {
            cout << "Initial balance cannot be negative, automatically set to 0" << endl;
            balance_ = 0.0;
        } else {
            balance_ = initial_balance;
        }
    }

    // Deposit operation (public interface)
    void deposit(double amount) {
        if (amount > 0) {
            balance_ += amount;
            cout << "Deposited " << amount << " successfully" << endl;
        } else {
            cout << "Deposit amount must be positive!" << endl;
        }
    }

    // Withdraw operation (public interface), returns success status
    bool withdraw(double amount) {
        if (amount <= 0) {
            cout << "Withdrawal amount must be positive!" << endl;
            return false;
        }
        if (amount > balance_) {
            cout << "Insufficient balance, withdrawal failed! (Current balance: " << balance_ << ")" << endl;
            return false;
        }
        balance_ -= amount;
        cout << "Withdrew " << amount << " successfully" << endl;
        return true;
    }

    // Get balance (read-only access, const ensures no modification to object state)
    double getBalance() const {
        return balance_;
    }
};

int main() {
    // 1. Create account object with initial balance of 1000
    BankAccount myAccount(1000.0);
    cout << "====== Initial State ======" << endl;
    cout << "Current balance: " << myAccount.getBalance() << endl;

    // 2. Deposit operation
    cout << "\n====== Deposit 500 ======" << endl;
    myAccount.deposit(500);
    cout << "Current balance: " << myAccount.getBalance() << endl;

    // 3. Normal withdrawal
    cout << "\n====== Withdraw 200 ======" << endl;
    if (myAccount.withdraw(200)) {
        cout << "Withdrawal completed" << endl;
    }
    cout << "Current balance: " << myAccount.getBalance() << endl;

    // 4. Attempt to withdraw more than balance (demonstrates encapsulation protecting data integrity)
    cout << "\n====== Attempt to withdraw 2000 (exceeds balance) ======" << endl;
    if (!myAccount.withdraw(2000)) {
        cout << "Withdrawal rejected, balance unchanged" << endl;
    }
    cout << "Current balance: " << myAccount.getBalance() << endl;

    // 5. Attempt illegal deposit (negative)
    cout << "\n====== Attempt to deposit -100 ======" << endl;
    myAccount.deposit(-100);
    cout << "Current balance: " << myAccount.getBalance() << endl;

    // 6. Demonstrate encapsulation key point: cannot directly access private members
    // Uncommenting the line below will cause compilation error (demonstrates information hiding)
    // myAccount.balance_ = 9999;  // Error: 'balance_' is private

    cout << "\n====== Final Account Balance ======" << endl;
    cout << myAccount.getBalance() << endl;

    return 0;
}

results matching ""

    No results matching ""