-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencapsulation.cpp
More file actions
48 lines (39 loc) · 974 Bytes
/
encapsulation.cpp
File metadata and controls
48 lines (39 loc) · 974 Bytes
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
#include <iostream>
using namespace std;
class Bankaccount {
private:
double Balance;
public:
Bankaccount(double initBal) {
if (initBal > 0) {
Balance = initBal;
} else {
Balance = 0;
}
}
void deposit(double amount) {
if (amount > 0) { // Fixed syntax
Balance += amount;
} else {
cout << "Amount must be positive" << endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= Balance) {
Balance -= amount;
} else {
cout << "Withdrawal amount exceeded account balance" << endl;
}
}
double getBalance() {
return Balance;
}
};
int main() {
Bankaccount myAccount(100);
myAccount.deposit(2000);
cout << "Balance is: " << myAccount.getBalance() << endl;
myAccount.deposit(200);
cout << "Balance is: " << myAccount.getBalance() << endl;
return 0;
}