-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq20.cpp
60 lines (49 loc) · 1.43 KB
/
q20.cpp
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
/* WAP to transfer money from one account (acct1) to another account (acct2)
* Class: acc_info
* Data members: acctno, balance
* Public member functions: read(), show(), transfermoney() */
#include <iostream>
using namespace std;
class Acc_info {
int acctno;
float balance;
public:
Acc_info(int acctno, float balance) : acctno(acctno), balance(balance){;}
float read();
void show();
float transfermoney(float amount, Acc_info& receiver);
};
inline float Acc_info::read() {
return balance;
}
void Acc_info::show() {
cout << "Account balance for account number " << acctno << " is: " << \
read() << endl;
}
float Acc_info::transfermoney(float amount, Acc_info& receiver) {
if (balance < amount) {
cout << "Error: insufficient funds" << endl;
return 0;
}
balance -= amount;
receiver.balance += amount;
cout << "Success: " << amount << " transferred from " << acctno << \
" to " << receiver.acctno << endl;
return amount;
}
int main() {
Acc_info acc1(12345, 15000), acc2(54321, 5000);
float amount;
cout << "====== Pre transaction ======" << endl;
acc1.show();
acc2.show();
cout << endl;
cout << "Enter amount of money to be transferred: ";
cin >> amount;
acc1.transfermoney(amount, acc2);
cout <<endl;
cout << "====== Post transaction ======" << endl;
acc1.show();
acc2.show();
return 0;
}