Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #ifndef __ARRAY_H
  2. #define __ARRAY_H
  3.  
  4. #include "MainHeader.h"
  5.  
  6. template<class T>
  7. class Array
  8. {
  9. public:
  10.     void setDelimiter(char delimiter){m_delimiter = delimiter;}
  11.     int getLogicalSize() const {return m_logical_Size;}
  12.     int getPhsicalSize() const {return m_physical_Size;}
  13.     char getDelimiter() const { return m_delimiter; }
  14.  
  15.     T& operator[](int index){return this->m_arr[index]; }
  16.     const Array& operator=(const Array & other)
  17.     {
  18.         if (this != &other)
  19.         {
  20.             delete[] m_arr;
  21.             m_physical_Size = other.m_physical_Size;
  22.             m_logical_Size = other.m_logical_Size;
  23.             m_delimiter = other.m_delimiter;
  24.             m_arr = new T[m_physical_Size];
  25.             for (int i = 0; i < m_logical_Size; i++)
  26.                 m_arr[i] = other.m_arr[i];
  27.         }
  28.         return *this;
  29.     }
  30.     const Array& operator+=(const T& newVal)
  31.     {
  32.         if (m_logical_Size < m_physical_Size)
  33.             m_arr[m_logical_Size++] = newVal;
  34.         else
  35.         {
  36.             auto newArr = new T[m_physical_Size * 2];
  37.             this->m_physical_Size = m_physical_Size * 2;
  38.             for (int i = 0; i < m_logical_Size; i++)
  39.                 newArr[i] = m_arr[i];
  40.             newArr[m_logical_Size++] = newVal;
  41.             delete[] m_arr;
  42.             m_arr = newArr;
  43.         }
  44.         return *this;
  45.     }
  46.     friend ostream& operator<<(ostream& os, const Array& arr)
  47.     {
  48.         for (int i = 0; i < m_arr.m_logical_Size; i++)
  49.             os << arr.arr[i] << arr.m_delimiter;
  50.         return os;
  51.     }
  52.  
  53.     Array(int maxSize = 2, char delimiter = ' ')
  54.     {
  55.         m_physical_Size = maxSize;
  56.         m_logical_Size = 0;
  57.         m_delimiter = delimiter;
  58.         m_arr = new T[m_physical_Size];
  59.     }
  60.     Array(const Array & other) : m_arr(nullptr)
  61.     {
  62.         *this = other;
  63.     }
  64.     ~Array();
  65.  
  66. private:
  67.     int m_physical_Size, m_logical_Size;
  68.     char m_delimiter;
  69.     T* m_arr;
  70. };
  71.  
  72. template<class T>
  73. Array<T>::~Array()
  74. {
  75.     delete[] m_arr;
  76. }
  77. #endif // !__ARRAY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement