-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.hpp
94 lines (81 loc) · 2.62 KB
/
Array.hpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jmartin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 15:24:29 by jmartin #+# #+# */
/* Updated: 2022/11/25 19:37:54 by jmartin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARRAY_HPP
# define ARRAY_HPP
# include <iostream>
# include <exception>
/*
* A template class is a class that can be used with different types of data.
*
* To be able to use a template class, we need to define the type of data that will be used with the class.
* eg:
* - Array<int> myArray; // myArray is an array of integers
* - Array<std::string> myArray; // myArray is an array of strings
*
*/
template <typename T>
class Array
{
public:
/* CONSTRUCTORS */
Array<T>(void) : _size(0) {
this->_array = new T[this->_size];
}
Array<T>(unsigned int n) : _size(n) {
this->_array = new T[this->_size];
}
/* COPY CONSTRUCTOR */
Array<T>(Array<T> const &src) : _size(src.size()){
this->_array = new T[this->_size];
for (unsigned int i = 0; i < this->_size; i++)
this->_array[i] = src._array[i];
}
/* DESTRUCTORS */
~Array<T>(void) {
if (this->_array)
delete [] this->_array;
}
/* OPERATORS */
T &operator[](unsigned int i) {
if (i >= this->_size || this->_array == NULL)
throw Array<T>::InvalidIndexException();
return (this->_array[i]);
}
Array<T> &operator=(Array<T> const &rhs) {
if (this != &rhs)
{
if (this->_array != NULL)
delete [] this->_array;
this->_size = rhs.size();
this->_array = new T[this->_size];
for (unsigned int i = 0; i < this->_size; i++)
this->_array[i] = rhs._array[i];
}
return (*this);
}
/* GETTERS / SETTERS */
unsigned int size(void) const {
return (this->_size);
}
/* EXCEPTIONS */
class InvalidIndexException : public std::exception
{
public:
virtual const char * what(void) const throw() {
return ("Error: Invalid index");
}
};
private:
T *_array;
unsigned int _size;
};
#endif