Advertisement
meta1211

Array

Sep 10th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3.  
  4. class Array
  5. {
  6. private:
  7.     int *arr;
  8.     int size;       //curent elements count
  9.     int capacity;   //max elements count
  10.  
  11.     void CopyElements(int *source, int len);
  12.  
  13.     void CopyElements(int *source, int len, int start);
  14.  
  15.     void Shift(int index)
  16.     {
  17.         for (int i = index; i < capacity; i++)
  18.         {
  19.             //TO DO SHIFT
  20.         }
  21.     }
  22.  
  23. public:
  24.     Array(int m = 0);
  25.     Array(int *b, int m);
  26.     Array(const Array &x);
  27.     ~Array();
  28.  
  29.     void Scan(int m);
  30.  
  31.     void Print();
  32.  
  33.     int & operator [](int i);
  34.  
  35.     int Find(int key);
  36.  
  37.     Array & operator = (Array &x);
  38.      
  39.     Array operator += (const int key);
  40.  
  41.     Array operator + (int key);
  42.  
  43.     Array operator += (Array a);
  44.  
  45.     Array operator + (Array a);
  46. };
  47.  
  48. //
  49.  
  50. #include "Array.h"
  51.  
  52. void Array::CopyElements(int *source, int len)
  53. {
  54.     for (int i = 0; i < len; i++)
  55.     {
  56.         arr[i] = source[i];
  57.     }
  58. }
  59.  
  60. void Array::CopyElements(int * source, int len, int start)
  61. {
  62.     for (int i = 0; i < len; i++)
  63.     {
  64.         arr[i + start] = source[i];
  65.     }
  66. }
  67.  
  68. #pragma region Contructors
  69.  
  70. Array::Array(int m)
  71. {
  72.     arr = new int[m];
  73.     size = 0;
  74.     capacity = m;
  75. }
  76.  
  77. Array::Array(int *b, int m)
  78. {
  79.     int *arr = b;
  80.     size = m;
  81.     capacity = m;
  82. }
  83.  
  84. Array::Array(const Array &x)
  85. {
  86.     arr = new int[x.capacity];
  87.     CopyElements(x.arr, x.size);
  88.     size = x.size;
  89.     capacity = x.capacity;
  90. }
  91.  
  92. Array::~Array()
  93. {
  94.     //delete arr;
  95. }
  96.  
  97. #pragma endregion
  98.  
  99. void Array::Scan(int m)
  100. {
  101.     for (int i = 0; i < m; i++)
  102.     {
  103.         std::cin >> arr[i + size];
  104.     }
  105.     size += m;
  106. }
  107.  
  108. void Array::Print()
  109. {
  110.     for (int i = 0; i < size; i++)
  111.     {
  112.         std::cout << arr[i] << ' ';
  113.     }
  114.     std::cout << '\n';
  115. }
  116.  
  117. int Array::Find(int key)
  118. {
  119.     for (int i = 0; i < size; i++)
  120.     {
  121.         if (arr[i] == key)
  122.         {
  123.             return i;
  124.         }
  125.     }
  126.     return -1;
  127. }
  128.  
  129. #pragma region Operators
  130.  
  131. int & Array::operator [](int i)
  132. {
  133.     if (i < capacity)
  134.     {
  135.         if (i > size - 1)
  136.             size = i + 1;
  137.         return arr[i];
  138.     }
  139.  
  140.     throw std::exception("Index out of array!");
  141. }
  142.  
  143. Array Array::operator += (const int key)
  144. {
  145.     arr[size] = key;
  146.     size++;
  147.     return *this;   //Why it calls Array::Array(Array &x)
  148. }
  149.  
  150. Array Array::operator + (int key)
  151. {
  152.     Array result(*this);
  153.     result += key;
  154.     return result;
  155. }
  156.  
  157. Array Array::operator += (Array a)
  158. {
  159.     CopyElements(a.arr, a.size, size);
  160.     return *this;
  161. }
  162.  
  163. Array Array::operator + (Array a)
  164. {
  165.     Array result(capacity + a.capacity);
  166.     result += *this;
  167.     result.size += size;
  168.     result += a;
  169.     std::cout << result[1] << '\n';
  170.     result.size += a.size;
  171.     return result;
  172. }
  173.  
  174. Array & Array::operator = (Array &x)
  175. {
  176.     delete arr;
  177.     arr = new int[x.capacity];
  178.     CopyElements(x.arr, x.size);
  179.     size = x.size;
  180.     capacity = x.capacity;
  181.     return *this;
  182. }
  183.  
  184. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement