Advertisement
krzotki

templates

Feb 21st, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "string"
  3. #include "iostream"
  4. #include "vector"
  5.  
  6. template<typename T,int N>
  7. class Array
  8. {
  9. private:
  10.     T values[N];
  11. public:
  12.     Array()
  13.     {
  14.         for (int i = 65;i < 65+N;i++)
  15.         {
  16.             values[i-65] = i;
  17.         }
  18.     }
  19.     int getSize()const
  20.     {
  21.         return N;
  22.     }
  23.     T* getValues()
  24.     {
  25.         return values;
  26.     }
  27.     void printValues()
  28.     {
  29.         for (int i = 0;i < N ;i++)
  30.         {
  31.             std::cout << values[i] << " ";
  32.         }
  33.         std::cout << std::endl;
  34.     }
  35. };
  36.  
  37. template<typename T>
  38. void Print(T value)
  39. {
  40.     std::cout << value << std::endl;
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.     Print(1);      
  47.     //Print<int>(1);
  48.  
  49.     Print(1.1f);
  50.     //Print<float>(1.1f);
  51.  
  52.     Print("jeden");
  53.     //Print<std::string>("jeden");
  54.  
  55.     Array<std::string,26> arr;
  56.  
  57.     std::cout << arr.getSize() << std::endl;
  58.     std::cout <<*(arr.getValues()+3) << std::endl;
  59.  
  60.     arr.printValues();
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement