home *** CD-ROM | disk | FTP | other *** search
-
- #include <iostream.h>
-
-
- const int True = 1;
- const int False = 0;
-
-
- template <class X> class ArrayType
- {
- protected:
- int maxItems; // maximum number of items this will hold
- int numItems; // number of items it is holding now
- X *items; // array of items
-
- public:
- ArrayType(int capacity);
- ~ArrayType() { delete [] items; }
-
- int InRange(int n) { return((n >= 0) && (n < numItems)); }
- int Capacity() { return maxItems; }
- int AddItem(X& toAdd);
- X& operator [] (int index) { return items[index]; }
- }
-
-
- template <class X> ArrayType<X>::ArrayType(int capacity)
- {
- maxItems = capacity;
- items = new X[capacity];
- numItems = 0;
- } // end ArrayType::ArrayType()
-
-
- template <class a> int ArrayType<a>::AddItem(a& toAdd)
- {
- if (numItems < maxItems)
- {
- items[numItems++] = toAdd;
- return True;
- }
-
- return False;
- } // end ArrayType::AddItem()
-
-
- template <class SomeType> size_t Size(const SomeType&)
- {
- return sizeof(SomeType);
- } // end Size()
-
-
- template <class D, class X> void Report(const char *s, D& d, X& x)
- {
- cout << endl << s << "has space for " << d.Capacity()
- << " items. Each item uses " << Size(x) << " bytes.\n";
-
- for (int i = 0; d.InRange(i); i++)
- cout << "\t" << s << "[" << i << "] " << d[i] << endl;
- } // end Report()
-
-
- int main()
- {
- int iVal;
- float fVal;
- ArrayType <int> iArray(3);
- ArrayType <float> fArray(5);
-
- for (iVal = 0, fVal = 0.0; iVal < 10; ++iVal, fVal += 1.11)
- {
- iArray.AddItem(iVal);
- fArray.AddItem(fVal);
- }
-
- Report("iArray: ", iArray, iArray[0]);
- Report("fArray: ", fArray, fArray[0]);
-
- return 0;
- }
-
-