-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass#5 function bank account.cpp
More file actions
49 lines (39 loc) · 2.1 KB
/
Copy pathclass#5 function bank account.cpp
File metadata and controls
49 lines (39 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
class BankAccount { // BankAccount ------- class name
private:
std::string accountNumber;
double balance;
public:
// Constructor to initialize BankAccount object with provided values
BankAccount(const std::string & accNum, double initialBalance): accountNumber(accNum), balance(initialBalance) {}
void deposit(double amount) {
balance += amount; // Add the deposited amount to the current balance
std::cout << "Deposit successful. Current balance: " << balance << std::endl; // Output success message and current balance
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount; // Deduct the withdrawn amount from the current balance
std::cout << "Withdrawal successful. Current balance: " << balance << std::endl;
} else {
std::cout << "Insufficient balance. Cannot withdraw." << std::endl;
}
}
};
int main() {
std::string sacno = "SB-123"; // Define the account number
double Opening_balance, deposit_amt, withdrawal_amt; // Define variables for opening balance, deposit amount, and withdrawal amount
Opening_balance = 1000; // Assign the opening balance
std::cout << "A/c. No." << sacno << " Balance: " << Opening_balance << std::endl; // Output the account details
BankAccount account(sacno, 1000.0); // Create a BankAccount object with initial account number and balance
deposit_amt = 1500;
std::cout << "Deposit Amount: " << deposit_amt << std::endl;
account.deposit(deposit_amt); // Call the deposit method of the account object
withdrawal_amt = 750;
std::cout << "Withdrawal Amount: " << withdrawal_amt << std::endl;
account.withdraw(withdrawal_amt); // Call the withdraw method of the account object
withdrawal_amt = 1800; // Define an amount higher than the balance for withdrawal
std::cout << "Attempt to withdrawal Amount: " << withdrawal_amt << std::endl; // Output the withdrawal amount
account.withdraw(withdrawal_amt); // Call the withdraw method of the account object
return 0;
}