Advertisement
LegoDrifter

Untitled

May 3rd, 2020
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5. class Array{
  6. private:
  7.     int * x;
  8.     int capacity;
  9.     int n;
  10. public:
  11.     Array(int capacity = 5)
  12.     {
  13.         this->capacity=capacity;
  14.         this->n=0;
  15.         x = new int [capacity]; // x ce bide dolg kolku sto e noviot capacity
  16.     }
  17.     // koga rabotime so DAM mora da imame copy constructor i operator =
  18.     Array(Array & a)
  19.     {
  20.         // pravime copy constructor
  21.         this->capacity=a.capacity;
  22.         this->n=a.n;
  23.         x = new int [a.capacity];
  24.         for(int i = 0; i < n ; i ++)
  25.         {
  26.             this->x[i] = a.x[i]; // so ova mu gi prefrluvame site elementi na copy constructorot
  27.         }
  28.  
  29.     }
  30.     Array & operator =(const Array & a)
  31.     {
  32.         if(this==&a)
  33.         return *this;
  34.         else
  35.         this->capacity=a.capacity;
  36.         this->n=a.n;
  37.         delete [] x; //mora da ja izbriseme memorijata na objektot koj ce dobie vrednost od drug objekt(Referensata)
  38.         x = new int [a.capacity];
  39.         for(int i = 0; i < n ; i ++)
  40.         {
  41.             this->x[i] = a.x[i]; // operator = e slicen so copy constructorot zato pravime samo copy paste
  42.         }
  43.         return *this;
  44.     }
  45.     ~Array()
  46.     {
  47.         delete [] x;
  48.     }
  49.     void add(int nov)
  50.     {
  51.         if(n==capacity)
  52.         {
  53.             int * temp = new int[capacity*2];
  54.             for(int i=0;i<n;i++)
  55.             {
  56.                 temp[i] = x[i];
  57.             }
  58.             capacity*=2;
  59.             delete [] x; //go brisime x za da ne pokazuvaat kon ista memorija
  60.             x = temp;
  61.         }
  62.         x[n] = nov;
  63.         n++;
  64.     }
  65.     void Change(int A,int B)
  66.     {
  67.         for(int i=0;i<n;i++)
  68.         {
  69.             if(x[i]==A)
  70.             {
  71.                 x[i]=B;
  72.             }
  73.         }
  74.     }
  75.     void DeleteAll(int za_brisenje)
  76.     {
  77.         int razlicnielementi = 0;
  78.         for(int i=0;i<n;i++)
  79.         {
  80.             if(x[i]!=za_brisenje)
  81.             razlicnielementi++;
  82.         }
  83.         int *temp = new int[razlicnielementi];
  84.         int j = 0;
  85.         for(int i=0;i<n;i++)
  86.         {
  87.             if(x[i]!=za_brisenje)
  88.             {
  89.                 temp[j]=x[i];
  90.                 j++;
  91.             }
  92.         }
  93.         delete [] x;
  94.         x=temp;
  95.         n=razlicnielementi;
  96.  
  97.     }
  98.     void print()
  99.     {
  100.         for(int i=0;i<n;i++)
  101.         {
  102.             cout<<x[i]<<" ";
  103.         }
  104.         cout<<endl;
  105.     }
  106.  
  107. };
  108.  
  109. int main()
  110. {
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement