Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ARRAYTEMPLATE_CPP
- #define ARRAYTEMPLATE_CPP
- //mora se dodati guard block i za cpp kod ovakve template klase
- #include "ArrayTemplate.h"
- #include <iostream>
- using namespace std;
- template <class T>
- ArrayTemplate<T>::ArrayTemplate(int c)
- {
- this->cap=c;
- this->br=0;
- this->data = new T[this->cap];
- }
- template <class T>
- ArrayTemplate<T>::ArrayTemplate(const ArrayTemplate<T>& a)
- {
- this->cap=a.cap;
- this->br=a.br;
- this->data = new T[cap];
- for (int i=0;i<cap;i++)
- {
- this->data[i]=a.data[i];
- }
- }
- template <class T>
- ArrayTemplate<T>& ArrayTemplate<T>::operator=(const ArrayTemplate<T>& a)
- {
- delete [] this->data;
- this->cap=a.cap;
- this->br=a.br;
- this->data = new T[cap];
- for (int i=0;i<cap;i++)
- {
- this->data[i]=a.data[i];
- }
- }
- template <class T>
- bool ArrayTemplate<T>::operator==(const ArrayTemplate<T>& a)
- {
- if (this->cap != a.cap)
- return false;
- if (this->br != a.br)
- return false;
- for(int i=0;i<this->br;i++)
- {
- if(this->data[i]!= a.data[i])
- return false;
- }
- return true;
- }
- template <class T>
- T& ArrayTemplate<T>::operator[](int index)
- {
- if ((index <0) || (index >= this->br))
- {
- throw 0;
- }
- return this->data[index];
- }
- template <class T>
- void ArrayTemplate<T>::Dodaj(T newElem)
- {
- if(this->br < this->cap)
- {
- this->data[this->br]=newElem;
- this->br++;
- }
- else
- {
- T* newData =new T[this->cap*2];
- for (int i=0;i<this->cap;i++)
- {
- newData[i]=this->data[i];
- }
- delete[] this->data;
- this->data=newData;
- this->cap*=2;
- this->data[this->br]=newElem;
- this->br++;
- }
- }
- template <class T>
- ArrayTemplate<T>::~ArrayTemplate()
- {
- delete [] this->data;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement