/*
school bag can keep books, pen eraser ,sharpner etc hidden in bag


Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden
from users. To achieve this, you must declare class variables/attributes as private
(cannot be accessed from outside the class). If you want others to read or modify 
the value of a private member, you can provide public get and set methods.

Why Encapsulation?
It is considered good practice to declare your class attributes as private 
(as often as you can). Encapsulation ensures better control of your data, 
because you (or others) can change one part of the code without affecting 
other parts
Increased security of data
*/

#include <iostream>
#include <string>

// Class BankAccount encapsulates both data (accountHolderName, balance) and methods (deposit, withdraw, getBalance)
class BankAccount {
private:
    // These are private members: they cannot be accessed directly outside the class.
    // This protects the data from being modified directly.
    std::string accountHolderName;
    double balance;

public:
    // Constructor to initialize the account holder's name and starting balance
    BankAccount(std::string name, double initialBalance) {
        accountHolderName = name;
        balance = initialBalance;
    }

    // Getter function to provide controlled access to the private member 'balance'
    // This is an example of encapsulation where we expose only what is necessary.
    double getBalance() {
        return balance;
    }

    // Public method to deposit money into the account.
    // This method encapsulates the logic to modify 'balance' and ensures only valid deposits are made.
    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            std::cout << "Deposited: " << amount << std::endl;
        } else {
            std::cout << "Invalid deposit amount!" << std::endl;
        }
    }

    // Public method to withdraw money from the account.
    // Like deposit, it encapsulates the withdrawal logic and checks if the withdrawal is valid.
    void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            std::cout << "Withdrew: " << amount << std::endl;
        } else {
            std::cout << "Invalid withdrawal amount!" << std::endl;
        }
    }
};

int main() {
    // Create a BankAccount object with an initial balance
    // Users interact with the account through public methods, not by directly accessing data.
    BankAccount account("John Doe", 500.0);

    // Call the public method 'deposit' to add money to the account
    account.deposit(200.0);

    // Call the public method 'withdraw' to take money out of the account
    account.withdraw(100.0);

    // Call the public method 'getBalance' to check the balance
    // This encapsulates how the balance is accessed, ensuring it can't be directly modified.
    std::cout << "Current Balance: " << account.getBalance() << std::endl;

    return 0;
}

/*

Explanation:
Private Members (accountHolderName, balance):

The data members (accountHolderName and balance) are declared as private. This means they cannot be accessed directly from outside the
class. Encapsulation is achieved by controlling access to these members through public methods.

Public Methods (deposit, withdraw, getBalance):

Public methods are provided to interact with the private data members.
The deposit method allows the user to add money to the account.
The withdraw method allows the user to take money out of the account.
The getBalance method allows the user to check the current balance.

    Encapsulation Benefits:

The private data (balance and accountHolderName) is protected from being modified in unexpected ways.
Users of the BankAccount class interact with the account through well-defined public methods, which ensures that the account's balance
    can only be modified in controlled ways.

    Key Points:
Data Protection: Private members are protected from outside interference.
Controlled Access: Public methods provide controlled access to the private data members.
Flexibility and Maintainability: The internal implementation can be changed without affecting code that uses the class, as long as the
public interface remains consistent.

*/

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: