|
ex7-4.cpp - Example 7-4 Inheritance - check and savings accounts |
// File: ex7-4.cpp
#include <iostream>
using namespace std;
class Account
{
protected:
long acct_no;
double balance;
double int_rate; // annual interest rate
public:
Account(long acc_no,double init_bal,double i_rate);
void deposit(double amount);
void withdraw(double amount);
void month_end(void);
void display(void);
};
Account::Account(long acc_no,double init_bal,double i_rate)
{
cout << "* New Account\t";
acct_no = acc_no;
balance = init_bal;
int_rate = i_rate;
display();
}
void Account::deposit(double amount)
{
cout << "Account: " << acct_no << " deposit = " << amount << endl;
balance += amount;
}
void Account::withdraw(double amount)
{
cout << "Account: " << acct_no << " withdraw = " << amount << endl;
balance -= amount;
}
void Account::display(void)
{
cout << "Account: " << acct_no << " balance = " << balance << endl << endl;
}
void Account::month_end(void)
{
cout << "Account month-end processing: " << acct_no << endl;
balance *= (1.+int_rate/12.);
display();
}
class SavingsAccount : public Account
{
public:
SavingsAccount(long acc_no, double init_bal = 50., double i_rate = .05)
: Account(acc_no,init_bal,i_rate) { }
};
class CheckingAccount : public Account
{
private:
double min_balance;
double service_charge;
public:
CheckingAccount(long, double, double = 300.,double = 2.,double = .04);
void process_check(double amt) { withdraw(amt); }
void month_end(void);
};
CheckingAccount::CheckingAccount(long acc_no, double init_bal,
double min_bal, double service_chg, double i_rate)
: Account(acc_no,init_bal,i_rate)
{
min_balance = min_bal;
service_charge = service_chg;
}
void CheckingAccount::month_end(void)
{
cout << "checking Account month-end processing: " << acct_no << endl;
balance *= (1.+int_rate/12.);
if (balance < min_balance) balance -= service_charge;
display();
}
int main(void)
{
SavingsAccount Mysavings(1234560L,500.);
CheckingAccount Mychecking(1234561L,1000.);
Mysavings.deposit(100.);
Mysavings.display();
Mysavings.withdraw(200.);
Mysavings.display();
Mychecking.deposit(100.);
Mychecking.display();
Mychecking.process_check(200.);
Mychecking.display();
Mysavings.month_end();
Mychecking.month_end();
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley