Docstoc

t13.3

Document Sample
t13.3 Powered By Docstoc
					Tutorial t13.3

Yishuai Qin

//Account.h

#include <iostream>
using namespace std;
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
public:
      Account(double);
      virtual void credit(double);
      virtual bool debit(double);
      void setBalance(double);
      double getBalance();
private:
      double balance;
};

#endif

//Account.cpp

#include <iostream>
using namespace std;

#include "Account.h"

Account::Account(double initialBalance)
{
      if(initialBalance>=0.0)
            balance=initialBalance;
      else
      {
            cout<<"Error: Initial balance cannot be negative."<<endl;
            balance=0.0;
      }
}

void Account::credit(double amount)
{
      balance=balance+amount;
}

bool Account::debit(double amount)
{
      if(amount>balance)
      {
            cout << "Debit amount exceeded account balance." << endl;
            return false;
      }
      else
         {
              balance=balance-amount;
              return true;
         }
}

void Account::setBalance(double newBalance)
{
      balance=newBalance;
}

double Account::getBalance()
{
      return balance;
}

//SavingsAccount.h

#ifndef SAVINGS_H
#define SAVINGS_H

#include"Account.h"
#include <iostream>
using namespace std;

class SavingsAccount:public Account
{
public:
      SavingsAccount(double,double);
      double calculateInterest();
private:
      double interestRate;
};

#endif


//SavingsAccount.cpp

#include <iostream>
using namespace std;
#include"SavingsAccount.h"

SavingsAccount::SavingsAccount(double initialBalance,double
initialInterestRate)
:Account(initialBalance)
{
      interestRate=initialInterestRate;
}

double SavingsAccount::calculateInterest()
{
      return getBalance()*interestRate;
}
//CheckingAccount.h

#include <iostream>
using namespace std;
#ifndef CHECKING_H
#define CHECKING_H

#include"Account.h"

class CheckingAccount:public Account
{
public:
      CheckingAccount(double,double);

      void credit(double);
      bool debit(double);
private:
      double transactionFee;
      void chargeFee();
};

#endif


//CheckingAccount.cpp

#include <iostream>
using namespace std;

#include"CheckingAccount.h"

CheckingAccount::CheckingAccount(double initialBalance,double initialFee)
:Account(initialBalance)
{
      transactionFee=(initialFee<0.0)?0.0:initialFee;
}

void CheckingAccount::credit(double amount)
{
      Account::credit(amount);
      chargeFee();
}

bool CheckingAccount::debit(double amount)
{
      bool success=Account::debit(amount);

         if(success)
         {
               chargeFee();
               return true;
         }
         else
               return false;
}
void CheckingAccount::chargeFee()
{
      Account::setBalance(getBalance()-transactionFee);
      cout<<"$"<<transactionFee<<"transaction fee charged"<<endl;
}


//polymorphBanking.cpp

#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;

#include"Account.h"
#include"SavingsAccount.h"
#include"CheckingAccount.h"

int main()
{
      vector <Account    *> accounts(4);
      accounts[0]=new    SavingsAccount(25.0,.03);
      accounts[1]=new    CheckingAccount(80.0,1.0);
      accounts[2]=new    SavingsAccount(200.0,.015);
      accounts[3]=new    CheckingAccount(400.0,.5);

        cout<<fixed<<setprecision(2);

        for(size_t i=0;i<accounts.size();i++)
        {
              cout<<"Account"<<i+1<<"balance:$"<<accounts[i]->getBalance();

              double withdrawalAmount=0.0;
              cout<<"\nEnter an amount to withdraw from Account"<<i+1<<":";
              cin>>withdrawalAmount;
              accounts[i]->debit(withdrawalAmount);

              double depositAmount=0.0;
              cout<<"Enter an amount to deposit into Account"<<i+1<<":";
              cin>>depositAmount;
              accounts[i]->credit(depositAmount);


        SavingsAccount*savingsAccountPtr=dynamic_cast<SavingsAccount*>(accounts
[i]);

            if(savingsAccountPtr!=0)
            {
                  double interestEarned=savingsAccountPtr-
>calculateInterest();
                  cout<<"Adding$"<<interestEarned<<"interest to
Account"<<i+1<<"(a SavingsAccount)"<<endl;
                  savingsAccountPtr->credit(interestEarned);
            }
            cout<<"Updated Account"<<i+1<<"balance:$"<<accounts[i]-
>getBalance()<<"\n\n";
      }
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:8/8/2012
language:
pages:5