ex02 done

This commit is contained in:
2026-03-22 21:41:58 +01:00
parent 5eae0db20d
commit b1b99275a7
3 changed files with 27 additions and 13 deletions

View File

View File

@@ -15,6 +15,8 @@
# define ARRAY_HPP # define ARRAY_HPP
# include <iostream> # include <iostream>
# include <cstdlib>
# include <ctime>
template <typename T> template <typename T>
class Array class Array

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */ /* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */
/* Updated: 2026/03/22 14:52:48 by aortigos ### ########.fr */ /* Updated: 2026/03/22 21:40:55 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -25,37 +25,49 @@ Array<T>::Array() : data(NULL), _size(0)
} }
template <typename T> template <typename T>
Array<T>::Array(unsigned int n) Array<T>::Array(unsigned int n) : _size(n)
{ {
data = new T[n];
} }
template <typename T> template <typename T>
Array(const Array &other) Array<T>::Array(const Array &other) : _size(other._size)
{ {
data = new T[other._size];
for (unsigned int i = 0;i < _size; i++)
data[i] = other.data[i];
} }
template <typename T> template <typename T>
Array &operator=(const Array &other) Array<T> &Array<T>::operator=(const Array &other)
{ {
if (this != &other)
{
delete[] data;
_size = other._size;
data = new T[_size];
for (unsigned int i = 0;i < _size; i++)
data[i] = other.data[i];
}
return (*this);
} }
template <typename T> template <typename T>
~Array() Array<T>::~Array()
{ {
delete[] data;
} }
template <typename T> template <typename T>
T &operator[](unsigned int i) T &Array<T>::operator[](unsigned int i)
{ {
if (i >= _size)
throw std::exception();
return (data[i]);
} }
template <typename T> template <typename T>
unsigned int size() const unsigned int Array<T>::size() const
{ {
return (_size);
} }