Files
cpp07/ex02/Array/Array.tpp
Angel Ortigosa Perez 317ecbc1f9 ex02 fixed
2026-04-08 20:51:29 +02:00

78 lines
2.2 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
/* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
//////////////////
// Constructors //
//////////////////
template <typename T>
Array<T>::Array() : data(NULL), length(0)
{
// std::cout << "Array default constructor called" << std::endl;
}
template <typename T>
Array<T>::Array(const Array &other)
{
data = new T[other.length]();
length = other.length;
for(unsigned int i = 0; i < length; i++)
{
data[i] = other.data[i];
}
// std::cout << "Array copy constructor called" << std::endl;
}
template <typename T>
Array<T> &Array<T>::operator=(const Array &other)
{
if (this != &other)
{
// Copy attributes here
delete[] data;
data = new T[other.length]();
length = other.length;
for(unsigned int i = 0; i < length; i++)
{
data[i] = other.data[i];
}
}
// std::cout << "Array copy assignment operator called" << std::endl;
return (*this);
}
template <typename T>
Array<T>::~Array()
{
delete[] data;
// std::cout << "Array destructor called" << std::endl;
}
template <typename T>
Array<T>::Array(unsigned int n) : length(n)
{
data = new T[n]();
}
template <typename T>
T &Array<T>::operator[](int n)
{
if (n < 0 || (unsigned int)n >= length)
throw std::exception();
return (data[n]);
}
template <typename T>
unsigned int Array<T>::size() const
{
return (this->length);
}