home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / komprese / zip / DELZIP12.ZIP / CDEMO1.ZIP / TESTB.TXT < prev    next >
Text File  |  1997-09-28  |  3KB  |  80 lines

  1.                             Sets in BCB
  2.  
  3.      Sets in BCB are an emulation of Pascal sets. They are 
  4.      implemented as a class template. Therefore this is easier 
  5.      to understand if you are familiar with both Pascal sets 
  6.      and C++ templates - but it's not essential.
  7.      
  8.      A set type is declared like this:
  9.      
  10.      typedef Set <element_type, minval, maxval> set_type;
  11.      
  12.      I will no doubt be shot down in flames for saying this, but
  13.      you can think of this as using a kind of super-macro which sets
  14.      up a class 'set_type', an instance of which can hold elements
  15.      of type 'element_type' between 'minval' and 'maxval'. That
  16.      falls a long way short of describing C++ templates but it's
  17.      good enough!
  18.      
  19.      Then you can declare instances of this set type as needed:
  20.      
  21.      set_type mySet;
  22.      
  23.      Borland have used overloaded stream operators << and >> to
  24.      permit adding elements to a set and removing them - the
  25.      analogy with streams is imperfect but does make sense.
  26.      
  27.      The following is a trivial console app which demonstrates
  28.      the basic use of Sets:
  29.      
  30.      //---------------------------------------------------------------------------
  31.      #include <vcl\condefs.h>
  32.      #include <stdio.h>
  33.      #include <stdlib.h>
  34.      #include <string.h>
  35.      
  36.      #include <vcl\sysdefs.h>       // needed for sets
  37.      #include <iostream.h>
  38.      #include <conio.h>
  39.      
  40.      #pragma hdrstop
  41.      //---------------------------------------------------------------------------
  42.      USERES("Project1.res");
  43.      //---------------------------------------------------------------------------
  44.      
  45.      typedef enum {apples, oranges, bananas, pears} fruit_t;
  46.      typedef Set < fruit_t, apples, pears > fruit_set;
  47.      
  48.      void ShowFruit(const fruit_set &fs);
  49.      
  50.      int main(int argc, char **argv)
  51.      {
  52.          fruit_set FruitBowl;                            // create the set
  53.          FruitBowl << apples << bananas << pears;        // initialise it
  54.          ShowFruit(FruitBowl);
  55.          FruitBowl >> apples;                            // take out apples
  56.          FruitBowl << oranges;                           // add oranges
  57.          ShowFruit(FruitBowl);
  58.          FruitBowl.Clear();                              // empty the bowl
  59.          ShowFruit(FruitBowl);
  60.      
  61.          getch();     //pause
  62.          return 0;
  63.      }
  64.      
  65.      void ShowFruit(const fruit_set &fs)
  66.      {
  67.      cout << "[ ";
  68.      if (fs.Contains(apples)) cout << "apples ";
  69.      if (fs.Contains(oranges)) cout << "oranges ";
  70.      if (fs.Contains(bananas)) cout << "bananas ";
  71.      if (fs.Contains(pears)) cout << "pears ";
  72.      cout << "]" << endl << endl;
  73.      }
  74.      
  75.      //=========================================
  76.      
  77.      The rest of the syntax for sets is in the online help. Good luck!
  78.      
  79.      Steve Balcombe
  80.