Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Array {
  6. private:
  7.     int* array;
  8.     int length;
  9. public:
  10.     Array();
  11.     Array(int* array, int length);
  12.     Array(int length, int value = 0);
  13.     ~Array();
  14.     Array& operator = (const Array& a);
  15.     friend ostream& operator<<(ostream& o, const Array& a);
  16.     Array& operator += (int element);
  17.     Array& operator -- ();
  18.     Array operator -- (int);
  19. };
  20.  
  21. Array::Array() {
  22.     this->array = new int;
  23.     *this->array = 0;
  24.     this->length = 1;
  25. }
  26.  
  27. Array::Array(int *array, int length) {
  28.     this->length = length;
  29.     this->array = new int[this->length];
  30.     for(int i = 0; i < length; i++) {
  31.         this->array[i] = array[i];
  32.     }
  33. }
  34.  
  35. Array::Array(int length, int value) {
  36.     this->length = length;
  37.     this->array = new int[this->length];
  38.     for(int i = 0; i < length; i++) {
  39.         this->array[i] = value;
  40.     }
  41. }
  42.  
  43. Array::~Array() {
  44.     delete [] this->array;
  45. }
  46.  
  47. Array& Array::operator=(const Array &a) {
  48.     if(this == &a) {
  49.         return *this;
  50.     }
  51.     delete [] this->array;
  52.     this->length = a.length;
  53.     this->array = new int[a.length];
  54.     for(int i = 0; i < this->length; i++) {
  55.         this->array[i] = a.array[i];
  56.     }
  57.     return *this;
  58. }
  59.  
  60. ostream& operator<<(ostream &o, const Array &a) {
  61.     for(int i = 0; i < a.length; i++) {
  62.         o << a.array[i] << " ";
  63.     }
  64.     o << endl;
  65.     return o;
  66. }
  67.  
  68. Array& Array::operator--() {
  69.     int* temp = new int[this->length - 1];
  70.     for(int i = 1; i < this->length; i++) {
  71.         temp[i - 1] = array[i];
  72.     }
  73.     delete [] this->array;
  74.     this->length--;
  75.     this->array = temp;
  76.     return *this;
  77. }
  78.  
  79. Array Array::operator--(int) {
  80.     Array old = Array(this->array, this->length);
  81.     int* temp = new int[this->length - 1];
  82.     for(int i = 1; i < this->length; i++) {
  83.         temp[i - 1] = array[i];
  84.     }
  85.     delete [] this->array;
  86.     this->length--;
  87.     this->array = temp;
  88.     return old;
  89. }
  90.  
  91. Array& Array::operator+=(int element) {
  92.     int* temp = new int[this->length + 1];
  93.     for(int i = 0; i < this->length; i++) {
  94.         temp[i] = this->array[i];
  95.     }
  96.     temp[this->length] = element;
  97.     this->length++;
  98.     delete [] this->array;
  99.     this->array = temp;
  100.     return *this;
  101. }
  102.  
  103. int main() {
  104.     int arr1[] = {3, 5, 7, 9};
  105.     Array ar1 = Array();
  106.     Array ar2 = Array(arr1, sizeof arr1 / sizeof (int));
  107.     Array ar3 = Array(5);
  108.     Array ar4 = Array(3, 4);
  109.     Array ar5;
  110.     ar5 = ar2;
  111.     cout << ar1;
  112.     cout << ar2;
  113.     cout << ar3;
  114.     ar2 += 21;
  115.     --ar1;
  116.     cout << ar1;
  117.     cout << ar2;
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement