home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ for Dummies (3rd Edition)
/
C_FD.iso
/
BUDGETS
/
BUDGET5.CPP
< prev
Wrap
C/C++ Source or Header
|
1996-09-15
|
7KB
|
279 lines
//BUDGET5.CPP - Budget program with inheritance and
// late binding (aka, polymorphism). Notice
// how much smaller the program is now that the
// redundancy has been removed. A single
// function can now handle both checking and
// savings accounts (and any other accounts that
// we might invent in the future).
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
//Account - this abstract class incorporates properties
// common to both account types: Checking and
// Savings. However, itÆs missing the concept
// withdrawal(), which is different between the two
class Account
{
protected:
Account(Account &c)
{
cout << "No creating funds\n";
}
public:
Account(unsigned accNo, float initialBalance = 0.0F);
//access functions
int accountNo()
{
return accountNumber;
}
float acntBalance()
{
return balance;
}
static Account *first()
{
return pFirst;
}
Account *next()
{
return pNext;
}
static int noAccounts()
{
return count;
}
//transaction functions
void deposit(float amount)
{
balance += amount;
} //Note 1
virtual void withdrawal(float amount) = 0;
//display function for displaying self on æcoutÆ
void display()
{
cout << "Account " << accountNumber
<< " = " << balance
<< "\n";
}
protected:
//keep accounts in a linked list so thereÆs no limit
static Account *pFirst; //Note 2
Account *pNext;
static int count; //number of accounts
unsigned accountNumber;
float balance;
};
//allocate space for statics //Note 2
Account *Account::pFirst = 0;
int Account::count = 0;
Account::Account(unsigned accNo, float initialBalance)
{
accountNumber = accNo;
balance = initialBalance;
//add this to end of list and count it
count++;
Account* pA;
if (pFirst == 0)
{
pFirst = this; //empty list; make it first
}
else //list not empty; look for last
{
for (pA = pFirst; pA->pNext; pA = pA->pNext)
{
}
pA->pNext = this; //tack us onto end
}
pNext = 0; //weÆre always last
}
//Checking - this class contains properties unique to
// checking accounts. Not much left, is there?
class Checking : public Account
{
public:
//here the constructor is defined inline
Checking(unsigned accNo, float initialBalance = 0.0F) :
Account(accNo, initialBalance) //Note 3
{
}
//overload pure virtual functions
virtual void withdrawal(float amount); //Note 4
};
void Checking::withdrawal(float amount) //Note 5
{
if (balance < amount )
{
cout << "Insufficient funds: balance " << balance
<< ", check " << amount
<< "\n";
}
else
{
balance -= amount;
//if balance falls too low, charge service fee
if (balance < 500.00F)
{
balance -= 0.20F;
}
}
}
//Savings - same story as Checking except that it also
// has a unique data member
class Savings : public Account
{
public:
//here the constructor is defined as a separate function
//just to show you the difference
Savings(unsigned accNo, float initialBalance = 0.0F);
//transaction functions
virtual void withdrawal(float amount);
protected:
int noWithdrawals;
};
//Note 6
Savings::Savings(unsigned accNo, float initialBalance) :
Account(accNo, initialBalance)
{
noWithdrawals = 0;
}
void Savings::withdrawal(float amount)
{
if (balance < amount)
{
cout << "Insufficient funds: balance " << balance
<< ", withdrawal " << amount
<< "\n";
}
else
{
if (++noWithdrawals > 1)
{
balance -= 5.00F;
}
balance -= amount;
}
}
//prototype declarations
unsigned getAccntNo();
void process(Account &account); //Note 7
void outOfMemory();
//main - accumulate the initial input and output totals
int main()
{
/*loop until someone enters æXÆ or æxÆ*/
Account *pA; //Note 8
char accountType; //S or C
unsigned keepLooping = 1;
while (keepLooping)
{
cout << "Enter S for Savings, "
"C for Checking, X for exit\n";
cin >> accountType;
switch (accountType)
{
case 'c':
case 'C':
pA = new Checking(getAccntNo());//Note 9
if (pA == 0)
{
outOfMemory();
}
process(*pA);
break;
case 's':
case 'S':
pA = new Savings(getAccntNo()); //Note 9
if (pA == 0)
{
outOfMemory();
}
process(*pA);
break;
case 'x':
case 'X':
keepLooping = 0;
break;
default:
cout << "I didn't get that.\n";
}
}
//now present totals //Note 10
float total = 0.0F;
cout << "Account totals:\n";
for (pA = Account::first(); pA; pA = pA->next())
{
pA->display();
total += pA->acntBalance();
}
cout << "Total worth = " << total << "\n";
return 0;
}
//getAccntNo - return the account number entered
unsigned getAccntNo()
{
unsigned accntNo;
cout << "Enter account number:";
cin >> accntNo;
return accntNo;
}
//process(Account) - input the data for an account
void process(Account &account)
{
cout << "Enter positive number for deposit,\n"
"negative for withdrawal, 0 to terminate";
float transaction;
do
{
cout << ":";
cin >> transaction;
//deposit
if (transaction > 0.0F)
{
account.deposit(transaction);
}
//withdrawal
if (transaction < 0.0F) {
account.withdrawal(-transaction); //Note 11
}
} while (transaction != 0.0F);
}
//outOfMemory - generate out-of-memory message and quit
void outOfMemory()
{
cout << "Out of memory\n";
abort();
}