Liny_@NotePad

沉迷ACG中

类模板练习

YOYO posted @ 2009年8月20日 02:00 in 【C/C++】 with tags 类模板 , 1723 阅读

 rt。。第一次玩这东西 = =

  1. #include <iostream>
  2.  
  3. template <class T>
  4. class CTem
  5. {
  6. public:
  7.         CTem(int i);
  8.         ~CTem(void)
  9.         {
  10.                 delete[] data;
  11.         }
  12.  
  13.         T& operator [](int i)
  14.         {
  15.                 return data[i];
  16.         }
  17.  
  18. private:
  19.         T* data;
  20. };
  21.  
  22. template <class T>
  23. CTem<T>::CTem(int n)
  24. {
  25.         data = new T[n];
  26. }
  27.  
  28. void main(void)
  29. {
  30.         CTem<int> tem(5);
  31.        
  32.         for(int i=0; i<5; i++)
  33.         {
  34.                 tem[i] = i+1;
  35.         }
  36.  
  37.         for(int i=0; i<5; i++)
  38.         {
  39.                 std::cout << tem[i] << " ";
  40.         }
  41.         std::cout << std::endl;
  42.  
  43.         system("pause");
  44. }
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter