-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.hpp
More file actions
67 lines (62 loc) · 2.1 KB
/
Array.hpp
File metadata and controls
67 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zelhajou <zelhajou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/01 21:38:58 by zelhajou #+# #+# */
/* Updated: 2025/01/02 20:33:25 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include <exception>
template <typename T>
class Array
{
private:
T *_elements;
unsigned int _size;
public:
Array() : _elements(NULL), _size(0) {}
Array(unsigned int n) : _elements(new T[n]()), _size(n) {}
Array(const Array &other) : _elements(new T[other._size]), _size(other._size)
{
for (unsigned int i = 0; i < _size; i++)
_elements[i] = other._elements[i];
}
~Array()
{
delete[] _elements;
}
Array &operator=(const Array &other)
{
if (this != &other)
{
delete[] _elements;
_size = other._size;
_elements = new T[_size];
for (unsigned int i = 0; i < _size; i++)
_elements[i] = other._elements[i];
}
return *this;
}
T &operator[](unsigned int index)
{
if (index >= _size)
throw std::out_of_range("Index out of bounds");
return _elements[index];
}
const T &operator[](unsigned int index) const
{
if (index >= _size)
throw std::out_of_range("Index out of bounds");
return _elements[index];
}
unsigned int size() const
{
return _size;
}
};
#endif