fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <typename TT1>
  6. class Vector{
  7. private:
  8. int size;
  9. TT1* ptr;
  10. public:
  11. Vector();
  12. ~Vector();
  13. Vector(int size);
  14. Vector(const Vector<TT1>& a);
  15. Vector<TT1>& operator=(const Vector<TT1>& a);
  16. void set(int position, TT1 data);
  17. TT1 get(int position);
  18. int get_size();
  19. };
  20.  
  21. //------Default Constructor------
  22. template <typename TT1>
  23. Vector<TT1> :: Vector() : size(0), ptr(NULL)
  24. {
  25. }
  26.  
  27. //-------Copy Constructor-------
  28. template <typename TT1>
  29. Vector<TT1> :: Vector(const Vector<TT1>& a) : size(0), ptr(NULL)
  30. {
  31. if(a.ptr != NULL)
  32. {
  33. this->size = a.size;
  34. // delete[] ptr;
  35. ptr = new TT1[size];
  36. for ( int i = 0; i < this->size; i++)
  37. this->ptr[i] = a.ptr[i];
  38. }
  39. }
  40.  
  41. //------Destructor-------
  42. template <typename TT1>
  43. Vector<TT1> :: ~Vector()
  44. {
  45. delete[] ptr;
  46. }
  47.  
  48. //-----Overloaded = operator----
  49. template <typename TT1>
  50. Vector<TT1>& Vector<TT1> :: operator=(const Vector<TT1>& a)
  51. {
  52. std::cout << "operator=, " << a.size << std::endl;
  53. if(this != &a)
  54. {
  55. this->size = a.size;
  56. if (this->ptr) delete[] this->ptr;
  57. this->ptr = new TT1[a.size];
  58. for (unsigned int i = 0; i < this->size; i++)
  59. this->ptr[i] = a.ptr[i];
  60. return *this;
  61. }
  62. }
  63. //----Constructor with size---
  64. template<typename TT1>
  65. Vector<TT1> :: Vector(int size)
  66. {
  67. this->size = size;
  68. ptr = new TT1[size];
  69. }
  70.  
  71. //---- Set data @ Poisition----
  72. template<typename TT1>
  73. void Vector<TT1> :: set(int position, TT1 data)
  74. {
  75. *(ptr+ position) = data;
  76. }
  77.  
  78. //----Get data From position----
  79. template<typename TT1>
  80. TT1 Vector<TT1> :: get(int position)
  81. {
  82. return *(ptr + position);
  83. }
  84.  
  85. //-----Get size----
  86. template<typename TT1>
  87. int Vector<TT1> :: get_size()
  88. {
  89. return size;
  90. }
  91.  
  92. void foo(Vector<string> a)
  93. {
  94.  
  95. }
  96.  
  97. int main()
  98. {
  99. Vector<string> a(3);
  100. a.set(0, "asd");
  101. a.set(2, "hjk");
  102. a.set(1, "34645!");
  103. for(int i = 0; i < a.get_size(); i++)
  104. {
  105. cout << a.get(i) << endl;
  106. }
  107. Vector<string> b = a;
  108. for(int i = 0; i < a.get_size(); i++)
  109. {
  110. cout << b.get(i) << endl;
  111. }
  112. foo(a);
  113.  
  114. return 0;
  115. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
asd
34645!
hjk
asd
34645!
hjk