Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- class Array
- {
- private:
- int *arr;
- int size; //curent elements count
- int capacity; //max elements count
- void CopyElements(int *source, int len);
- void CopyElements(int *source, int len, int start);
- void Shift(int index)
- {
- for (int i = index; i < capacity; i++)
- {
- //TO DO SHIFT
- }
- }
- public:
- Array(int m = 0);
- Array(int *b, int m);
- Array(const Array &x);
- ~Array();
- void Scan(int m);
- void Print();
- int & operator [](int i);
- int Find(int key);
- Array & operator = (Array &x);
- Array operator += (const int key);
- Array operator + (int key);
- Array operator += (Array a);
- Array operator + (Array a);
- };
- //
- #include "Array.h"
- void Array::CopyElements(int *source, int len)
- {
- for (int i = 0; i < len; i++)
- {
- arr[i] = source[i];
- }
- }
- void Array::CopyElements(int * source, int len, int start)
- {
- for (int i = 0; i < len; i++)
- {
- arr[i + start] = source[i];
- }
- }
- #pragma region Contructors
- Array::Array(int m)
- {
- arr = new int[m];
- size = 0;
- capacity = m;
- }
- Array::Array(int *b, int m)
- {
- int *arr = b;
- size = m;
- capacity = m;
- }
- Array::Array(const Array &x)
- {
- arr = new int[x.capacity];
- CopyElements(x.arr, x.size);
- size = x.size;
- capacity = x.capacity;
- }
- Array::~Array()
- {
- //delete arr;
- }
- #pragma endregion
- void Array::Scan(int m)
- {
- for (int i = 0; i < m; i++)
- {
- std::cin >> arr[i + size];
- }
- size += m;
- }
- void Array::Print()
- {
- for (int i = 0; i < size; i++)
- {
- std::cout << arr[i] << ' ';
- }
- std::cout << '\n';
- }
- int Array::Find(int key)
- {
- for (int i = 0; i < size; i++)
- {
- if (arr[i] == key)
- {
- return i;
- }
- }
- return -1;
- }
- #pragma region Operators
- int & Array::operator [](int i)
- {
- if (i < capacity)
- {
- if (i > size - 1)
- size = i + 1;
- return arr[i];
- }
- throw std::exception("Index out of array!");
- }
- Array Array::operator += (const int key)
- {
- arr[size] = key;
- size++;
- return *this; //Why it calls Array::Array(Array &x)
- }
- Array Array::operator + (int key)
- {
- Array result(*this);
- result += key;
- return result;
- }
- Array Array::operator += (Array a)
- {
- CopyElements(a.arr, a.size, size);
- return *this;
- }
- Array Array::operator + (Array a)
- {
- Array result(capacity + a.capacity);
- result += *this;
- result.size += size;
- result += a;
- std::cout << result[1] << '\n';
- result.size += a.size;
- return result;
- }
- Array & Array::operator = (Array &x)
- {
- delete arr;
- arr = new int[x.capacity];
- CopyElements(x.arr, x.size);
- size = x.size;
- capacity = x.capacity;
- return *this;
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement