home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1564.ZIP / TI1564.ASC
Text File  |  1993-10-05  |  6KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1564
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 5, 1993                          PAGE  :  1/5
  12.  
  13.     TITLE  :  BIDS BI_ArrayAsVector example
  14.  
  15.  
  16.  
  17.  
  18.   #include  <alloc.h>
  19.   #include  <arrays.h>
  20.   #include  <conio.h>
  21.   #include <iostream.h>   // cout
  22.   #include <strstrea.h>   // ostrstream
  23.   #include <string.h>     // strlen, strcpy
  24.  
  25.  
  26.   /*
  27.     Class Peep
  28.  
  29.     Description:
  30.       A simple class containing an integral value and a
  31.       dynamically allocated character array.  A Peep is a member
  32.       of a group and has the characteristics of an identifying
  33.       number and a name.
  34.   */
  35.   class Peep
  36.   {
  37.   public:
  38.     Peep (const char * peepName, unsigned peepID=0);
  39.     Peep (const Peep &);
  40.     Peep (void);
  41.  
  42.     ~Peep (void);
  43.  
  44.     friend void TestPeep (void);
  45.     Peep & operator = (Peep &peep);
  46.     int operator == (Peep &peep);
  47.     int operator < (Peep &peep);
  48.     int operator != (Peep &peep);
  49.     int isSortable (void);
  50.  
  51.     const char * Name() { return name; }
  52.     const unsigned ID() { return id; }
  53.  
  54.   private:
  55.     unsigned id;
  56.     char * name;
  57.     static const char * defPeepName;
  58.   };
  59.  
  60.   // Peeps with noname get this name.
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1564
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 5, 1993                          PAGE  :  2/5
  78.  
  79.     TITLE  :  BIDS BI_ArrayAsVector example
  80.  
  81.  
  82.  
  83.  
  84.   //
  85.   const char * Peep::defPeepName = "NoName";
  86.  
  87.  
  88.   //  Function operator<< provides streaming of class Peep.
  89.   //
  90.   ostream& operator<<( ostream& os, Peep& peep )
  91.   {
  92.     return (os << peep.ID() << "::" << peep.Name());
  93.   }
  94.  
  95.   //  Struct DefPeeps provides some data for us to play with.
  96.   //
  97.   struct DefPeeps
  98.   {
  99.     static const char * peepNames[];
  100.     static const int peepCount;
  101.   };
  102.  
  103.   const char *DefPeeps::peepNames[] =
  104.     {
  105.       "one", "two", "three", "four", "five", "six", "seven",
  106.       "eight", "nine", "ten", "eleven", "twelve", "thirteen",
  107.       "fourteen", "fifteen", "sixteen"
  108.     };
  109.  
  110.   // Number of peepNames in our example data.
  111.   //
  112.   const DefPeeps::peepCount =
  113.           ( sizeof( peepNames ) / sizeof( peepNames[0] ) );
  114.  
  115.   const bufLen = 130;
  116.  
  117.  
  118.   //  Function main - build an array of Peeps and display the list.
  119.   //
  120.   int main (int, char **argv)
  121.   {
  122.     unsigned  char i;
  123.  
  124.     cout << "Testing " << argv[0] << " built " << __DATE__
  125.           << " " << __TIME__ << endl;
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1564
  141.   VERSION  :  All
  142.        OS  :  All
  143.      DATE  :  October 5, 1993                          PAGE  :  3/5
  144.  
  145.     TITLE  :  BIDS BI_ArrayAsVector example
  146.  
  147.  
  148.  
  149.  
  150.     //  Declare a simple array of pointers to Peep objects.
  151.     //
  152.     BI_ArrayAsVector<Peep>  a(DefPeeps::peepCount, 0, 5);
  153.  
  154.     /*
  155.       Load the array with some Peeps.  These peeps are created
  156.       on the heap.
  157.     */
  158.     for (i = 0; i < DefPeeps::peepCount; i++)
  159.     {
  160.       Peep  *p;
  161.  
  162.       p = new Peep (DefPeeps::peepNames[i]);
  163.       a.add (*p);
  164.       delete p; // Peep was copied, remove original peep from heap.
  165.     }
  166.  
  167.     //  Display results
  168.     //
  169.     cout << "Your Peeps are:" << endl << endl;
  170.  
  171.     for (i = 0; i < DefPeeps::peepCount; i++)
  172.       cout << a[i] << endl;  // Calls our operator << for peeps.
  173.  
  174.     //  Cleanup
  175.     //
  176.     a.flush();  // Clean peeps out of Array.
  177.  
  178.     cout << endl << endl << "======================" << endl;
  179.  
  180.     return 0;
  181.   }
  182.  
  183.  
  184.   /*
  185.     Implementation of class Peep
  186.     Constructor for class Peep initializes id and name. If an id
  187.     is not provided, it will be set to zero.
  188.   */
  189.   Peep::Peep( const char * peepName, unsigned peepID ) :
  190.     id (peepID)
  191.   {
  192.     name = new char [strlen (peepName) + 1];
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1564
  207.   VERSION  :  All
  208.        OS  :  All
  209.      DATE  :  October 5, 1993                          PAGE  :  4/5
  210.  
  211.     TITLE  :  BIDS BI_ArrayAsVector example
  212.  
  213.  
  214.  
  215.  
  216.     if (name)
  217.       strcpy( name, peepName );
  218.   }
  219.  
  220.   // Copy constructor.
  221.   //
  222.   Peep::Peep (const Peep &peep)
  223.   {
  224.     name  = new char [strlen (peep.name) + 1];
  225.     CHECK (name != NULL);
  226.     strcpy (name, peep.name);
  227.     id  = peep.id;
  228.   }
  229.  
  230.   // Default constructor.
  231.   //
  232.   Peep::Peep (void)
  233.   {
  234.     name  = new char[strlen (Peep::defPeepName) + 1];
  235.     CHECK (name != NULL);
  236.     strcpy (name, Peep::defPeepName);
  237.     id  = 0;
  238.   }
  239.  
  240.   //  Destructor for class Peep deletes name.
  241.   //
  242.   Peep::~Peep (void)
  243.   {
  244.     delete name;
  245.   }
  246.  
  247.   Peep & Peep::operator = (Peep &peep)
  248.   {
  249.     int i;
  250.  
  251.     delete name;
  252.  
  253.     i = strlen (peep.name) + 1;
  254.     name  = new char[i];
  255.     CHECK (name != NULL);
  256.     strcpy (name, peep.name);
  257.     id  = peep.id;
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1564
  273.   VERSION  :  All
  274.        OS  :  All
  275.      DATE  :  October 5, 1993                          PAGE  :  5/5
  276.  
  277.     TITLE  :  BIDS BI_ArrayAsVector example
  278.  
  279.  
  280.  
  281.  
  282.     return *this;
  283.   }
  284.  
  285.   int Peep::operator == (Peep &peep)
  286.   {
  287.     if  (!strcmp (name, peep.name) && (id == peep.id))
  288.       return 1;
  289.  
  290.     else
  291.       return 0;
  292.   }
  293.  
  294.   int Peep::operator < (Peep &peep)
  295.   {
  296.     if  ((strcmp (name, peep.name) < 0) || (id < peep.id))
  297.       return 1;
  298.  
  299.     else
  300.       return 0;
  301.   }
  302.  
  303.   int Peep::operator != (Peep &peep)
  304.   {
  305.     if  (strcmp (name, peep.name) || (id != peep.id))
  306.       return 1;
  307.     else
  308.       return 0;
  309.   }
  310.  
  311.   int Peep::isSortable (void)
  312.   {
  313.     return 0;
  314.   }
  315.  
  316.  
  317.   DISCLAIMER: You have the right to use this technical information
  318.   subject to the terms of the No-Nonsense License Statement that
  319.   you received with the Borland product to which this information
  320.   pertains.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.