simple real life example of code using abstraction for a banking system in c++

1 answer

Here's a simple example of using abstraction in a banking system in C++:

```cpp
#include <iostream>
using namespace std;

// Abstract class for bank account
class BankAccount {
public:
virtual void deposit(double amount) = 0;
virtual void withdraw(double amount) = 0;
virtual void displayBalance() = 0;
};

// Concrete class for savings account
class SavingsAccount : public BankAccount {
private:
double balance;
public:
SavingsAccount(double initialBalance) : balance(initialBalance) {}

void deposit(double amount) {
balance += amount;
}

void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
cout << "Insufficient funds" << endl;
}
}

void displayBalance() {
cout << "Savings Account Balance: $" << balance << endl;
}
};

int main() {
BankAccount *account = new SavingsAccount(1000);

account->deposit(500);
account->displayBalance();

account->withdraw(200);
account->displayBalance();

delete account;

return 0;
}
```

In this code, we have an abstract class `BankAccount` which defines the interface for a bank account with methods for depositing, withdrawing, and displaying the balance. We then have a concrete class `SavingsAccount` that implements these methods for a savings account.

In the `main` function, we create a new `SavingsAccount` object and use it to deposit and withdraw funds, displaying the balance after each operation. We use abstraction to define the common interface for different types of bank accounts, allowing us to interact with them in a consistent way without needing to know the specific details of each account type.
Similar Questions
    1. answers icon 1 answer
    1. answers icon 1 answer
  1. What is abstraction? Describe it in your OWN words.Give an example from your life where you use or have used abstraction.
    1. answers icon 1 answer
  2. PLEASE HELP1.Create a diagram of the U.S banking system and the Federal Reserve System(FRS). Include special financial services
    1. answers icon 1 answer
more similar questions