-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_4.cpp
More file actions
96 lines (84 loc) · 2.48 KB
/
4_4.cpp
File metadata and controls
96 lines (84 loc) · 2.48 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Transaction {
string type;
double amount;
};
class BankAccount {
protected:
int accountNumber;
double balance;
vector<Transaction> history;
public:
BankAccount(int accNum, double bal) : accountNumber(accNum), balance(bal) {}
virtual ~BankAccount() {}
virtual void deposit(double amount) {
balance += amount;
history.push_back({"deposit", amount});
}
virtual void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
history.push_back({"withdraw", amount});
} else {
cout << "Insufficient balance\n";
}
}
void undoLastTransaction() {
if (!history.empty()) {
Transaction last = history.back();
history.pop_back();
if (last.type == "deposit") {
balance -= last.amount;
} else if (last.type == "withdraw") {
balance += last.amount;
}
} else {
cout << "No transaction to undo\n";
}
}
void showBalance() const {
cout << "Account Number: " << accountNumber << ", Balance: " << balance << endl;
}
};
class SavingsAccount : public BankAccount {
double interestRate;
public:
SavingsAccount(int accNum, double bal, double rate) : BankAccount(accNum, bal), interestRate(rate) {}
void applyInterest() {
double interest = balance * interestRate / 100;
deposit(interest);
}
};
class CurrentAccount : public BankAccount {
double overdraftLimit;
public:
CurrentAccount(int accNum, double bal, double limit) : BankAccount(accNum, bal), overdraftLimit(limit) {}
void withdraw(double amount) override {
if (balance + overdraftLimit >= amount) {
balance -= amount;
history.push_back({"withdraw", amount});
} else {
cout << "Overdraft limit exceeded\n";
}
}
};
int main() {
SavingsAccount sa(1001, 5000, 3.5);
sa.deposit(1000);
sa.withdraw(700);
sa.applyInterest();
sa.showBalance();
sa.undoLastTransaction();
sa.showBalance();
CurrentAccount ca(2001, 3000, 1000);
ca.withdraw(3500);
ca.deposit(500);
ca.showBalance();
ca.undoLastTransaction();
ca.showBalance();
cout<<endl<<"24CE052_pushti"<<endl;
return 0;
}