home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / sumup / sumup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  2.8 KB  |  76 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*-------------------------------------------------------------*\
  19. |  sumup.CPP  - Sum up integers to demonstrate using iterators. |
  20. |                                                    """""""""  |
  21. |  Add all elements in a bag to demonstrate the use of          |
  22. |  iterators with additional arguments.                         |
  23. |  The bag elements are integers passed as arguments to main(). |
  24. \*-------------------------------------------------------------*/
  25.  
  26.    #include <ibag.h>
  27.    #include <iostream.h>
  28.    #include <stdlib.h>
  29.  
  30.    typedef IBag <int> IntBag;
  31.  
  32.    class SumApplicator : public IConstantApplicator <int> {
  33.      int ivSum;
  34.    public:
  35.      SumApplicator () : ivSum (0) {}
  36.      IBoolean applyTo (int const& i) {
  37.        ivSum += i;
  38.        return True;
  39.      }
  40.      int sum () { return ivSum; }
  41.    };
  42.  
  43.  
  44.    int sumUsingApplicatorObject (IntBag const& bag) {
  45.      SumApplicator sumUp;
  46.      bag.allElementsDo (sumUp);
  47.      return sumUp.sum ();
  48.    }
  49.  
  50.  
  51.    IBoolean sumUpFunction (int const& i, void* sum) {
  52.      *(int*)sum += i;
  53.      return True;
  54.    };
  55.  
  56.    int sumUsingApplicatorFunction (IntBag const& bag) {
  57.      int sum = 0;
  58.      bag.allElementsDo (sumUpFunction, &sum);
  59.      return sum;
  60.    }
  61.  
  62.  
  63. int main (int argc, char* argv[])  {
  64.      IntBag intbag;
  65.      for (int cnt=1; cnt < argc; cnt++)
  66.         intbag.add(atoi(argv[cnt]));
  67.  
  68.      cout << "Sum obtained using an Applicator Object = "
  69.           <<  sumUsingApplicatorObject(intbag)  << endl;
  70.  
  71.      cout << "Sum obtained using an Applicator Function = "
  72.           <<  sumUsingApplicatorFunction(intbag)  << endl;
  73.  
  74.      return 0;
  75.      }
  76.