home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C16 / Stemp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  704 b   |  33 lines

  1. //: C16:Stemp.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Simple template example
  7. #include "../require.h"
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. template<class T>
  12. class Array {
  13.   static const int size = 100;
  14.   T A[size];
  15. public:
  16.   T& operator[](int index) {
  17.     require(index >= 0 && index < size);
  18.     return A[index];
  19.   }
  20. };
  21.  
  22. int main() {
  23.   Array<int> ia;
  24.   Array<float> fa;
  25.   for(int i = 0; i < 20; i++) {
  26.     ia[i] = i * i;
  27.     fa[i] = float(i) * 1.414;
  28.   }
  29.   for(int j = 0; j < 20; j++)
  30.     cout << j << ": " << ia[j]
  31.          << ", " << fa[j] << endl;
  32. } ///:~
  33.