Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class intArray
- {
- private:
- int size, capacity, *a;
- public:
- intArray(int n = 0, int val = 0);
- intArray(const intArray &);
- ~intArray();
- void push_back(int val);
- intArray & operator = (const intArray &);
- };
- intArray::intArray(int n, int val)
- {
- a = nullptr;
- capacity = size = 0;
- if(n > 0){
- a = new int[n];
- capacity = size = n;
- for(int i = 0; i < size; ++i)
- a[i] = val;
- }
- }
- intArray::intArray(const intArray & src)
- {
- a = nullptr;
- capacity = size = 0;
- if(src.size > 0){
- a = new int[src.size];
- capacity = size = src.size;
- for(int i = 0; i < size; ++i)
- a[i] = src.a[i];
- }
- }
- intArray::~intArray()
- {
- if(capacity > 0)
- delete [] a;
- }
- intArray & intArray::operator = (const intArray & src)
- {
- if(capacity < src.capacity){
- if(capacity > 0)
- delete [] a;
- a = new int[src.capacity];
- capacity = src.size;
- }
- size = src.size;
- for(int i = 0; i < size; ++i)
- a[i] = src.a[i];
- }
- void intArray::push_back(int val)
- {
- if(size == capacity)
- {
- int new_capacity = capacity > 0 ? 2 * capacity : 1;
- int * new_a = new int[new_capacity];
- for(int i = 0; i < size; ++i)
- new_a[i] = a[i];
- delete [] a;
- a = new_a;
- capacity = new_capacity;
- }
- a[size++] = val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement