Advertisement
razvanth21

Untitled

Nov 22nd, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class IntegerArray
  6. {
  7.     private:
  8.         int *arr;
  9.         int size, maxSize = 10;
  10.        
  11.     public:
  12.         IntegerArray();
  13.         IntegerArray(int elements[], int len);
  14.         ~IntegerArray();
  15.         IntegerArray& operator =(const IntegerArray &a);
  16.         string to_string();
  17.         void put(int e);
  18.         void remove(int idx);
  19. };
  20.  
  21. void IntegerArray::put(int e)
  22. {  
  23.     if (size + 1 > maxSize)
  24.     {
  25.         // dublam valoarea maxima pe care o poate tine array-ul
  26.         maxSize *= 2;
  27.        
  28.         // alocam memorie pt un tablou temporar pt a muta datele vechi
  29.         int *tempArr = new int[maxSize];
  30.         for (int i = 0; i < size; i++)
  31.         {
  32.             tempArr[i] = arr[i];
  33.         }
  34.        
  35.         delete []arr;
  36.         arr = tempArr;
  37.     }
  38.    
  39.     arr[size++] = e;
  40. }
  41.  
  42. void IntegerArray::remove(int idx)
  43. {
  44.     if (idx < 0 || idx > size - 1)
  45.     {
  46.         cout << "invalid index passed!" << endl;
  47.         return;
  48.     }
  49.    
  50.     if (arr[idx] == NULL)
  51.     {
  52.         cout << "that index does not have any valued stored!" << endl;
  53.     }
  54.    
  55.     for (int i = idx; i < size - 1; i++)
  56.     {
  57.         arr[i] = arr[i + 1];
  58.     }
  59.    
  60.     arr[--size] = NULL;
  61. }
  62.  
  63. IntegerArray::IntegerArray(int elements[], int len)
  64. {
  65.     if (len > maxSize)
  66.     {
  67.         arr = new int[len];
  68.     }
  69.     else
  70.     {
  71.         arr = new int[maxSize];
  72.     }
  73.    
  74.     for (int i = 0; i < len; i++)
  75.     {
  76.         arr[i] = elements[i];
  77.     }
  78.    
  79.     size = len;
  80. }
  81.  
  82. string IntegerArray::to_string()
  83. {
  84.     std::string str = "[";
  85.    
  86.     for (int i = 0; i < size; i++)
  87.     {
  88.         str += std::to_string(arr[i]);
  89.         if (i < size - 1)
  90.         {
  91.             str += ", ";
  92.         }
  93.     }
  94.    
  95.     str += "]";
  96.     return str;
  97. }
  98.  
  99. IntegerArray::IntegerArray()
  100. {
  101.     arr = new int[maxSize];
  102.     size = 0;
  103. }
  104.  
  105. IntegerArray& IntegerArray::operator =(const IntegerArray &rhs)
  106. {
  107.     // verificare in cazul in care se face o atribuire de genul: 'a = a;'
  108.     if (this != &rhs)
  109.     {
  110.         // dealocarea memoriei vechiului obiect
  111.         delete []arr;
  112.        
  113.         // alocarie memorie noua
  114.         arr = new int[rhs.size];
  115.        
  116.         // copiarea valorilor obiectului vechi in noul obiect
  117.         size = rhs.size;
  118.         for (int i = 0; i < size; i++)
  119.         {
  120.             arr[i] = rhs.arr[i];
  121.         }
  122.     }
  123.     return *this;
  124. }
  125.  
  126. IntegerArray::~IntegerArray()
  127. {
  128.     delete []arr;
  129. }
  130.  
  131. class DescriptedIntegerArray: public IntegerArray
  132. {
  133.     private:
  134.         string description;
  135.    
  136.     public:
  137.         DescriptedIntegerArray(const DescriptedIntegerArray &rhs);
  138.         DescriptedIntegerArray& operator =(const DescriptedIntegerArray &rhs);
  139. };
  140.  
  141. DescriptedIntegerArray::DescriptedIntegerArray(const DescriptedIntegerArray &rhs) : IntegerArray(rhs), description(rhs.description)
  142. {
  143.     cout << "copy constructor" << endl;
  144. }
  145.  
  146. DescriptedIntegerArray& DescriptedIntegerArray::operator =(const DescriptedIntegerArray &rhs)
  147. {
  148.     description = rhs.description;
  149.     return *this;
  150. }
  151.  
  152. int main(void)
  153. {
  154.     int arr[] = {1, 2, 3, 4};
  155.     IntegerArray a(arr, sizeof(arr) / sizeof(*arr));
  156.     a.put(5);
  157.     a.put(1);
  158.     a.put(2);
  159.     a.put(15);
  160.     a.put(2);
  161.     a.put(11);
  162.     a.put(12);
  163.     a.remove(3);
  164.     cout << a.to_string() << endl;
  165.    
  166.     IntegerArray b;
  167.     b = a;
  168.     b.remove(5);
  169.     b = b;
  170.     cout << b.to_string() << endl;
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement