home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / !gcc / examples / cc / template < prev   
Encoding:
Text File  |  2002-02-14  |  772 b   |  65 lines

  1. /* Template repository demonstration.  */
  2.  
  3. #include <cstdio>
  4.  
  5. template<class T> class stack {
  6.   T* v;
  7.   T* p;
  8.   int sz;
  9. public:
  10.   stack(int);
  11.   ~stack();
  12.  
  13.   void push (T);
  14.   T pop();
  15.  
  16.   int size() const;
  17. };
  18.  
  19. template<class T> void stack<T>::push(T a)
  20. {
  21.   *p++ = a;
  22. }
  23.  
  24. template<class T> stack<T>::stack(int s)
  25. {
  26.   v = p = new T[sz=s];
  27. }
  28.  
  29. template<class T> stack<T>::~stack()
  30. {
  31.   delete[] v;
  32. }
  33.  
  34. template<class T> T stack<T>::pop()
  35. {
  36.   return *--p;
  37. }
  38.  
  39. template<class T> int stack<T>::size() const
  40. {
  41.   return p-v;
  42. }
  43.  
  44. stack<int> ip(200);
  45.  
  46. void f(stack<int>& sc)
  47. {
  48.   sc.push(10);
  49.   int z = 4*sc.pop();
  50.  
  51.   stack<char>*p = 0;
  52.   p = new stack<char>(800);
  53.  
  54.   for (int i = 0; i < 400; i++) {
  55.     p->push(i);
  56.   }
  57. }
  58.  
  59. int main (void)
  60. {
  61.   f (ip);
  62.   printf ("It worked\n");
  63.   return 0;
  64. }
  65.