40 lines
945 B
C++
40 lines
945 B
C++
#pragma once
|
|
|
|
#include <iostream>
|
|
|
|
template <typename T>
|
|
class Array {
|
|
public:
|
|
Array() : arr(new T[0]), len(0) {};
|
|
Array(unsigned int len) : arr(new T[len]), len(len) {};
|
|
Array(const Array &other) : arr(new T[other.size()]), len(other.size()) {
|
|
for (unsigned int i = 0; i < other.size(); i++)
|
|
arr[i] = other.arr[i];
|
|
}
|
|
~Array() {delete[] arr;}
|
|
Array &operator=(const Array &other) {
|
|
if (*this != &other) {
|
|
delete[] this->arr;
|
|
arr = new T[other.size()];
|
|
len = other.size();
|
|
for (unsigned int i = 0; i < other.size(); i++)
|
|
arr[i] = other.arr[i];
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
T& operator[](unsigned int i) const {
|
|
return i >= len ? throw OutOfBoundException() : arr[i];
|
|
}
|
|
|
|
unsigned int size() const {return len;}
|
|
|
|
class OutOfBoundException : public std::exception {
|
|
public: const char* what() const throw() { return "Index is out of bounds"; }
|
|
};
|
|
|
|
private:
|
|
T *arr;
|
|
unsigned int len;
|
|
};
|