Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Array{
- private:
- int * x;
- int capacity;
- int n;
- public:
- Array(int capacity = 5)
- {
- this->capacity=capacity;
- this->n=0;
- x = new int [capacity]; // x ce bide dolg kolku sto e noviot capacity
- }
- // koga rabotime so DAM mora da imame copy constructor i operator =
- Array(Array & a)
- {
- // pravime copy constructor
- this->capacity=a.capacity;
- this->n=a.n;
- x = new int [a.capacity];
- for(int i = 0; i < n ; i ++)
- {
- this->x[i] = a.x[i]; // so ova mu gi prefrluvame site elementi na copy constructorot
- }
- }
- Array & operator =(const Array & a)
- {
- if(this==&a)
- return *this;
- else
- this->capacity=a.capacity;
- this->n=a.n;
- delete [] x; //mora da ja izbriseme memorijata na objektot koj ce dobie vrednost od drug objekt(Referensata)
- x = new int [a.capacity];
- for(int i = 0; i < n ; i ++)
- {
- this->x[i] = a.x[i]; // operator = e slicen so copy constructorot zato pravime samo copy paste
- }
- return *this;
- }
- ~Array()
- {
- delete [] x;
- }
- void add(int nov)
- {
- if(n==capacity)
- {
- int * temp = new int[capacity*2];
- for(int i=0;i<n;i++)
- {
- temp[i] = x[i];
- }
- capacity*=2;
- delete [] x; //go brisime x za da ne pokazuvaat kon ista memorija
- x = temp;
- }
- x[n] = nov;
- n++;
- }
- void Change(int A,int B)
- {
- for(int i=0;i<n;i++)
- {
- if(x[i]==A)
- {
- x[i]=B;
- }
- }
- }
- void DeleteAll(int za_brisenje)
- {
- int razlicnielementi = 0;
- for(int i=0;i<n;i++)
- {
- if(x[i]!=za_brisenje)
- razlicnielementi++;
- }
- int *temp = new int[razlicnielementi];
- int j = 0;
- for(int i=0;i<n;i++)
- {
- if(x[i]!=za_brisenje)
- {
- temp[j]=x[i];
- j++;
- }
- }
- delete [] x;
- x=temp;
- n=razlicnielementi;
- }
- void print()
- {
- for(int i=0;i<n;i++)
- {
- cout<<x[i]<<" ";
- }
- cout<<endl;
- }
- };
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement