home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / CHAP19 / CHAP19_2.CPP < prev    next >
C/C++ Source or Header  |  1996-09-02  |  2KB  |  58 lines

  1. // Chap19_2.cpp
  2. // This is not a complete program. Notice, however, the use
  3. // of dynamic_cast.
  4. class Account
  5. {
  6.   public:
  7.     Account(int accNo, float initialBalance);
  8.  
  9.     virtual void deposit(float amount);
  10.     virtual void withdrawal(float amount);
  11.  
  12.     // addAccount - add current account to the account list
  13.     void addAccount();
  14.  
  15.     // getNext - get the next account from the account list;
  16.     //           if pPrev == 0, return first;
  17.     //           return 0 when no more left in list
  18.     static Account* getNext(Account *pPrev);
  19. };
  20.  
  21. class Checking : public Account
  22. {
  23.   public:
  24.     Checking(int accNo, float initialBalance = 0.0F);
  25. };
  26.  
  27. class Savings : public Account
  28. {
  29.   public:
  30.     Savings(int accNo, float initialBalance = 0.0F);
  31.  
  32.     // addInterest - calculate the interest for one time
  33.     //               period and add it back in
  34.     void addInterest();
  35. };
  36.  
  37. // accInterest - loop thru the accounts. For each Savings
  38. //               account you find, calculate the interest and
  39. //               it back in
  40. void accInterest()
  41. {
  42.     // loop thru the Accounts
  43.     Account* pA = (Account*)0;
  44.     while(pA = Account::getNext(pA))
  45.     {
  46.         // only process Savings accounts
  47.         // make a type-safe downcast:
  48.         Savings* pS = dynamic_cast<Savings*>(pA);
  49.  
  50.         // if pS is non-zero then this really was a Savings
  51.         if (pS)
  52.         {
  53.             // so add the interest in
  54.             pS->addInterest();
  55.         }
  56.     }
  57. }
  58.