-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencapsulation2.cpp
More file actions
40 lines (40 loc) · 979 Bytes
/
Copy pathencapsulation2.cpp
File metadata and controls
40 lines (40 loc) · 979 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
#include <iostream>
using namespace std;
class Account{
private:
double balance;
public:
Account(double amount){ //constructor
balance = amount;
}
~Account(){ //destructor
cout << "Destructor is called\n";
}
//setter method/function
void deposit(double amount){
if(amount > 0){
balance += amount;
}
}
//setter method/function
void withdraw(double amount){
if(amount > 0 && balance >= amount){
balance -= amount;
}
}
//getter method/function
double getBalance(){
return balance;
}
};
int main(){
Account a1(5000); //Constructor called automatically when object is created
cout << "Initial deposit: " << a1.getBalance() << endl;
a1.deposit(10000); //deposit function called
cout << "After 2nd deposit: " << a1.getBalance() << endl;
a1.deposit(7000); //deposit function called
cout << "After 3rd deposit: " << a1.getBalance() << endl;
a1.withdraw(2000); //withdraw function called
cout << "Current Balance: " << a1.getBalance() << endl;
return 0;
}