Advertisement
edensheiko

C++ Class array ( just for fun)

Feb 26th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. // there is some bugs with += and = operators
  2. //Array.h
  3. #pragma once
  4. class Array
  5. {
  6. public:
  7.     Array(const Array &);
  8.     ~Array();
  9.     Array(int= 10); // defult size of array
  10.     int GetSize()const;
  11.     void Print() const;
  12.     int find(const int) const;
  13.     Array& operator=(const Array&);
  14.     bool operator==(const Array&)const;
  15.     Array& operator+=(const Array& a);
  16.     int& operator[](int);
  17.     operator int() { return numofElements; }
  18. private:
  19.     int numofElements;
  20.     int *p;
  21. };
  22. //Array.cpp
  23. #include "Array.h"
  24. #include <assert.h>
  25. #include <iostream>
  26. #include <iomanip>
  27.  
  28. using namespace std;
  29.  
  30.  
  31. Array::Array(const Array & n) //copy cons
  32. {
  33.     this->numofElements = n.numofElements;
  34.     this->p = new int[numofElements];
  35.     assert(p);
  36.     for (int i = 0; i < numofElements; i++)
  37.     {
  38.         this->p[i] = n.p[i];
  39.     }
  40. }
  41.  
  42. Array::~Array()
  43. {
  44.     delete [] p;
  45.     assert(p);
  46.     p = NULL;
  47.    
  48. }
  49.  
  50. Array::Array(int size)
  51. {
  52.     numofElements = size;
  53.     p = new int[size];
  54.     assert(p);
  55.     for (int i = 0; i < size; i++)
  56.     {
  57.         p[i] = 0; //calloc
  58.     }
  59.  
  60. }
  61.  
  62. int Array::GetSize() const
  63. {
  64.     return this->numofElements;
  65. }
  66.  
  67. void Array::Print() const
  68. {
  69.     for (int i = 0; i < numofElements; i++)
  70.     {
  71.         cout << setw(3) << p[i];
  72.     }
  73.     cout << endl;
  74. }
  75.  
  76. int Array::find(const int x) const
  77. {
  78.     for (int i = 0; i < numofElements; i++)
  79.     {
  80.         if (p[i]==x)
  81.         {
  82.             return 1;
  83.         }
  84.         return -1;
  85.     }
  86. }
  87.  
  88. Array & Array::operator=(const Array& n) // a3=a4
  89. {
  90.     if (this->numofElements!=n.numofElements)
  91.     {
  92.         delete[] p;
  93.         p = NULL;
  94.         p = new int[n.numofElements];
  95.         assert(p);
  96.     }
  97.     for (int i = 0; i < numofElements; i++)
  98.     {
  99.         p[i] = n.p[i];
  100.     }
  101.     return *this; //return ptr to the "array"
  102. }
  103.  
  104. bool Array::operator==(const Array &n)const //
  105. {
  106.     if (this->numofElements != n.numofElements)
  107.     {
  108.         return false;
  109.     }
  110.     else
  111.     {
  112.         for (int i = 0; i < numofElements; ++i)
  113.         {
  114.             if (p[i]!=n.p[i])
  115.             {
  116.                 return false;
  117.             }
  118.             return true;
  119.         }
  120.     }
  121. }
  122.  
  123. Array& Array::operator+=(const Array& a)
  124. {
  125.     Array temp = *this;
  126.     delete[]p;
  127.     p = new int[numofElements + a.numofElements];
  128.     assert(p);
  129.     for (int i = 0; i < numofElements; i++)
  130.         p[i] = temp.p[i];
  131.     for (int i = 0; i < numofElements+a.numofElements; i++)
  132.         p[i] = a.p[i - numofElements];
  133.     numofElements = numofElements + a.numofElements;
  134.     return *this;
  135. }
  136.  
  137. int& Array::operator[](const int n)
  138. {
  139.     return p[n];
  140. }
  141. //Main.cpp
  142. #include <iostream>
  143. #include "Array.h"
  144.  
  145. using namespace std;
  146.  
  147. void main()
  148. {
  149.     Array a1;
  150.     Array a2(5);
  151.     a2.GetSize();
  152.     a1.Print();
  153.     a2.Print();
  154.     for (int i = 0; i < a1.GetSize(); i++)
  155.     {
  156.         a1[i] = i+1;
  157.     }
  158.     a1.Print();
  159.     Array a3(50);
  160.     a3.Print();
  161.     a1.find(5);
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement