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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Account {
public:
Account();
Account(double bal);
~Account(); // destructor
void deposit(double amount);
void withdraw(double amount);
double get_balance() const;
private:
double balance;
};
#endif /* ACCOUNT_H */
//======================================
#ifndef BANK_H
#define BANK_H
#include <string>
using namespace std;
#include "Account.h"
class Bank {
public:
Bank();
Bank(double checking_amount, double savings_amount);
~Bank(); // Destructor
void deposit(double amount, string account);
void withdraw(double amount, string account);
void transfer(double amount, string account);
void print_balances() const;
private:
Account checking;
Account savings;
};
#endif /* BANK_H */
//===========================================
#include "Bank.h"
//Bank::Bank();
Bank::Bank(double checking_amount, double savings_amount)
{
checking=checking_amount;
savings=savings_amount;
}
void deposit(double amount, string account)
{
if(account=="S")
{
savings=savings+amount;
}
else if(account=="C")
{
checking=checking+amount;
}
else;
}
void withdraw(double amount, string account)
{
if(account=="S")
{
savings=savings-amount;
}
else if(account=="C")
{
checking=checking-amount;
}
else;
}
void transfer(double amount, string account)
{
if(account=="S")
{
checking=savings-amount;
savings=checking+amount;
}
else if(account=="C")
{
savings=checking-amount;
checking=savings+amount;
}
else;
}
void print_balances() const
{
cout<<"Savings account balance: $"<<checking<<endl;
cout<<"Checking account balance: $"<<savings<<endl;
}
Bank::~Bank()
{
}
//==========================================
#include <iostream>
using namespace std;
#include "Bank.h"
int main() {
Bank my_bank;
cout << "Inital bank balances: " << endl;
my_bank.print_balances(); /* set up empty accounts */
cout << "Adding some money to accounts: " << endl;
my_bank.deposit(1000, "S"); /* deposit $1000 to savings */
my_bank.deposit(2000, "C"); /* deposit $2000 to checking */
my_bank.print_balances();
cout << "Taking out $1500 from checking,and moving $200 from";
cout << " savings to checking." << endl;
my_bank.withdraw(1500, "C"); /* withdraw $1500 from checking */
my_bank.transfer(200, "S"); /* transfer $200 from savings */
my_bank.print_balances();
cout << "Trying to transfer $900 from Checking." << endl;
my_bank.transfer(900, "C");
my_bank.print_balances();
cout << "trying to transfer $900 from Savings." << endl;
my_bank.transfer(900, "S");
my_bank.print_balances();
return 0;
}
//=====================================
|