/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Array.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/05 08:50:55 by narnaud #+# #+# */ /* Updated: 2022/09/05 11:23:19 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include template class Array { T *tab; int nbr; public: Array() { tab = new T[0]; }; Array(int n) { tab = new T[nbr = n]; }; Array(const Array &a) { tab = new T[nbr = a.size()]; for (int i = 0; i < nbr; i++) { tab[i] = a[i]; } }; ~Array() { delete [] tab; }; Array &operator= (const Array &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); }; };