Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class IntegerArray
- {
- private:
- int *arr;
- int size, maxSize = 10;
- public:
- IntegerArray();
- IntegerArray(int elements[], int len);
- ~IntegerArray();
- IntegerArray& operator =(const IntegerArray &a);
- string to_string();
- void put(int e);
- void remove(int idx);
- };
- void IntegerArray::put(int e)
- {
- if (size + 1 > maxSize)
- {
- // dublam valoarea maxima pe care o poate tine array-ul
- maxSize *= 2;
- // alocam memorie pt un tablou temporar pt a muta datele vechi
- int *tempArr = new int[maxSize];
- for (int i = 0; i < size; i++)
- {
- tempArr[i] = arr[i];
- }
- delete []arr;
- arr = tempArr;
- }
- arr[size++] = e;
- }
- void IntegerArray::remove(int idx)
- {
- if (idx < 0 || idx > size - 1)
- {
- cout << "invalid index passed!" << endl;
- return;
- }
- if (arr[idx] == NULL)
- {
- cout << "that index does not have any valued stored!" << endl;
- }
- for (int i = idx; i < size - 1; i++)
- {
- arr[i] = arr[i + 1];
- }
- arr[--size] = NULL;
- }
- IntegerArray::IntegerArray(int elements[], int len)
- {
- if (len > maxSize)
- {
- arr = new int[len];
- }
- else
- {
- arr = new int[maxSize];
- }
- for (int i = 0; i < len; i++)
- {
- arr[i] = elements[i];
- }
- size = len;
- }
- string IntegerArray::to_string()
- {
- std::string str = "[";
- for (int i = 0; i < size; i++)
- {
- str += std::to_string(arr[i]);
- if (i < size - 1)
- {
- str += ", ";
- }
- }
- str += "]";
- return str;
- }
- IntegerArray::IntegerArray()
- {
- arr = new int[maxSize];
- size = 0;
- }
- IntegerArray& IntegerArray::operator =(const IntegerArray &rhs)
- {
- // verificare in cazul in care se face o atribuire de genul: 'a = a;'
- if (this != &rhs)
- {
- // dealocarea memoriei vechiului obiect
- delete []arr;
- // alocarie memorie noua
- arr = new int[rhs.size];
- // copiarea valorilor obiectului vechi in noul obiect
- size = rhs.size;
- for (int i = 0; i < size; i++)
- {
- arr[i] = rhs.arr[i];
- }
- }
- return *this;
- }
- IntegerArray::~IntegerArray()
- {
- delete []arr;
- }
- class DescriptedIntegerArray: public IntegerArray
- {
- private:
- string description;
- public:
- DescriptedIntegerArray(const DescriptedIntegerArray &rhs);
- DescriptedIntegerArray& operator =(const DescriptedIntegerArray &rhs);
- };
- DescriptedIntegerArray::DescriptedIntegerArray(const DescriptedIntegerArray &rhs) : IntegerArray(rhs), description(rhs.description)
- {
- cout << "copy constructor" << endl;
- }
- DescriptedIntegerArray& DescriptedIntegerArray::operator =(const DescriptedIntegerArray &rhs)
- {
- description = rhs.description;
- return *this;
- }
- int main(void)
- {
- int arr[] = {1, 2, 3, 4};
- IntegerArray a(arr, sizeof(arr) / sizeof(*arr));
- a.put(5);
- a.put(1);
- a.put(2);
- a.put(15);
- a.put(2);
- a.put(11);
- a.put(12);
- a.remove(3);
- cout << a.to_string() << endl;
- IntegerArray b;
- b = a;
- b.remove(5);
- b = b;
- cout << b.to_string() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement