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
|
// This is the class declaration. It could be in it's own file, e.g. BankAccount.h
// use header guards if it's in a separate file, like so:
// #ifndef BANK_ACCOUNT_H
// #define BANK_ACCOUNT_H
/**
A bank account whose balance can be changed by deposits and withdrawals.
*/
class BankAccount
{
public:
/**
Constructs a bank account with zero balance.
*/
BankAccount();
/**
Constructs a bank account with a given balance.
@param initial_balance the initial balance
*/
BankAccount(double initial_balance);
/**
Makes a deposit into this account.
@param amount the amount of the deposit
*/
void deposit(double amount);
/**
Makes a withdrawal from this account, or charges a penalty if
sufficient funds are not available.
@param amount the amount of the withdrawal
*/
void withdraw(double amount);
/**
Adds interest to this account.
@param rate the interest rate in percent
*/
void add_interest(double rate);
/**
Gets the current balance of this bank account.
@return the current balance
*/
double get_balance() const;
private:
double balance;
};
// use header guards if in a separate file, like so:
// #endif //BANK_ACCOUNT_H
*******************************************************************
// This is the implementation of the class declared above
// If the class is declared in a separate file, e.g. BankAccount.h,
// then this should also be in its own file, e.g. BankAccount.cpp
// it then follows that the header file must be included here like so:
// #include "BankAccount.h"
double BankAccount::get_balance() const
{
return balance;
}
void BankAccount::deposit(double amount)
{
balance = balance + amount;
}
void BankAccount::withdraw(double amount)
{
const double PENALTY = 10;
if (amount > balance)
{
balance = balance - PENALTY;
}
else
{
balance = balance - amount;
}
}
void BankAccount::add_interest(double rate)
{
double amount = balance * rate / 100;
deposit(amount);
}
BankAccount::BankAccount()
{
balance = 0;
}
BankAccount::BankAccount(double initial_balance)
{
balance = initial_balance;
}
*******************************************************************
// This is the test program, again this could be in a separate file,
// or all 3 could be in one .cpp file. notice the standard library
// headers included, for cout, endl, fixed and setprecision
// If it's in a file by itself, the BankAccount class must be declared,
// by including the header like so:
// #include "BankAccount.h"
#include <iostream>
#include <iomanip>
int main()
{
BankAccount harrys_account(1000);
harrys_account.deposit(500); // Balance is now $1500
harrys_account.withdraw(2000); // Balance is now $1490
harrys_account.add_interest(1); // Balance is now $1490 + 14.90
std::cout << std::fixed << std::setprecision(2)
<< harrys_account.get_balance() << std::endl;
return 0;
}
|