You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
62 lines
1.7 KiB
2 years ago
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
|
/* Array.hpp :+: :+: :+: */
|
||
|
/* +:+ +:+ +:+ */
|
||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2022/09/05 08:50:55 by narnaud #+# #+# */
|
||
|
/* Updated: 2022/09/05 11:23:19 by narnaud ### ########.fr */
|
||
|
/* */
|
||
|
/* ************************************************************************** */
|
||
|
|
||
|
#include <iostream>
|
||
|
|
||
|
template <class T> class Array {
|
||
|
T *tab;
|
||
|
int nbr;
|
||
|
public:
|
||
|
Array() {
|
||
|
tab = new T[0];
|
||
|
};
|
||
|
|
||
|
Array(int n) {
|
||
|
tab = new T[nbr = n];
|
||
|
};
|
||
|
|
||
|
Array(const Array<T> &a) {
|
||
|
tab = new T[nbr = a.size()];
|
||
|
for (int i = 0; i < nbr; i++) {
|
||
|
tab[i] = a[i];
|
||
|
}
|
||
|
};
|
||
|
|
||
|
~Array() {
|
||
|
delete [] tab;
|
||
|
};
|
||
|
|
||
|
Array<T> &operator= (const Array<T> &a) {
|
||
|
delete [] tab;
|
||
|
nbr = a.size();
|
||
|
tab = new T[nbr];
|
||
|
for (int i = 0; i < nbr; i++) {
|
||
|
tab[i] = a[i];
|
||
|
}
|
||
|
return (*this);
|
||
|
};
|
||
|
|
||
|
T &operator[] (int i) {
|
||
|
if (i >= nbr) throw (std::runtime_error("error: Array index out of bound."));
|
||
|
return (tab[i]);
|
||
|
};
|
||
|
|
||
|
T operator[] (int i) const {
|
||
|
if (i >= nbr) throw (std::runtime_error("error: Array index out of bound."));
|
||
|
return (tab[i]);
|
||
|
};
|
||
|
|
||
|
int size() const{
|
||
|
return (nbr);
|
||
|
};
|
||
|
};
|