PRODUCT : Borland C++ NUMBER : 1564 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/5 TITLE : BIDS BI_ArrayAsVector example #include #include #include #include // cout #include // ostrstream #include // strlen, strcpy /* Class Peep Description: A simple class containing an integral value and a dynamically allocated character array. A Peep is a member of a group and has the characteristics of an identifying number and a name. */ class Peep { public: Peep (const char * peepName, unsigned peepID=0); Peep (const Peep &); Peep (void); ~Peep (void); friend void TestPeep (void); Peep & operator = (Peep &peep); int operator == (Peep &peep); int operator < (Peep &peep); int operator != (Peep &peep); int isSortable (void); const char * Name() { return name; } const unsigned ID() { return id; } private: unsigned id; char * name; static const char * defPeepName; }; // Peeps with noname get this name. PRODUCT : Borland C++ NUMBER : 1564 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/5 TITLE : BIDS BI_ArrayAsVector example // const char * Peep::defPeepName = "NoName"; // Function operator<< provides streaming of class Peep. // ostream& operator<<( ostream& os, Peep& peep ) { return (os << peep.ID() << "::" << peep.Name()); } // Struct DefPeeps provides some data for us to play with. // struct DefPeeps { static const char * peepNames[]; static const int peepCount; }; const char *DefPeeps::peepNames[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen" }; // Number of peepNames in our example data. // const DefPeeps::peepCount = ( sizeof( peepNames ) / sizeof( peepNames[0] ) ); const bufLen = 130; // Function main - build an array of Peeps and display the list. // int main (int, char **argv) { unsigned char i; cout << "Testing " << argv[0] << " built " << __DATE__ << " " << __TIME__ << endl; PRODUCT : Borland C++ NUMBER : 1564 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/5 TITLE : BIDS BI_ArrayAsVector example // Declare a simple array of pointers to Peep objects. // BI_ArrayAsVector a(DefPeeps::peepCount, 0, 5); /* Load the array with some Peeps. These peeps are created on the heap. */ for (i = 0; i < DefPeeps::peepCount; i++) { Peep *p; p = new Peep (DefPeeps::peepNames[i]); a.add (*p); delete p; // Peep was copied, remove original peep from heap. } // Display results // cout << "Your Peeps are:" << endl << endl; for (i = 0; i < DefPeeps::peepCount; i++) cout << a[i] << endl; // Calls our operator << for peeps. // Cleanup // a.flush(); // Clean peeps out of Array. cout << endl << endl << "======================" << endl; return 0; } /* Implementation of class Peep Constructor for class Peep initializes id and name. If an id is not provided, it will be set to zero. */ Peep::Peep( const char * peepName, unsigned peepID ) : id (peepID) { name = new char [strlen (peepName) + 1]; PRODUCT : Borland C++ NUMBER : 1564 VERSION : All OS : All DATE : October 25, 1993 PAGE : 4/5 TITLE : BIDS BI_ArrayAsVector example if (name) strcpy( name, peepName ); } // Copy constructor. // Peep::Peep (const Peep &peep) { name = new char [strlen (peep.name) + 1]; CHECK (name != NULL); strcpy (name, peep.name); id = peep.id; } // Default constructor. // Peep::Peep (void) { name = new char[strlen (Peep::defPeepName) + 1]; CHECK (name != NULL); strcpy (name, Peep::defPeepName); id = 0; } // Destructor for class Peep deletes name. // Peep::~Peep (void) { delete name; } Peep & Peep::operator = (Peep &peep) { int i; delete name; i = strlen (peep.name) + 1; name = new char[i]; CHECK (name != NULL); strcpy (name, peep.name); id = peep.id; PRODUCT : Borland C++ NUMBER : 1564 VERSION : All OS : All DATE : October 25, 1993 PAGE : 5/5 TITLE : BIDS BI_ArrayAsVector example return *this; } int Peep::operator == (Peep &peep) { if (!strcmp (name, peep.name) && (id == peep.id)) return 1; else return 0; } int Peep::operator < (Peep &peep) { if ((strcmp (name, peep.name) < 0) || (id < peep.id)) return 1; else return 0; } int Peep::operator != (Peep &peep) { if (strcmp (name, peep.name) || (id != peep.id)) return 1; else return 0; } int Peep::isSortable (void) { return 0; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.