Project made from zero

This commit is contained in:
aortigos
2026-03-23 14:45:44 +01:00
parent 582f5ad276
commit d05d0063a7
7 changed files with 122 additions and 141 deletions

View File

@@ -5,13 +5,12 @@
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:04:01 by aortigos #+# #+# */
/* Updated: 2026/03/21 17:04:01 by aortigos ### ########.fr */
/* Created: 2026/03/23 14:28:55 by aortigos #+# #+# */
/* Updated: 2026/03/23 14:28:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARRAY_HPP
# define ARRAY_HPP
# include <iostream>
@@ -19,24 +18,23 @@
# include <ctime>
template <typename T>
class Array
class Array
{
private:
T *data;
unsigned int _size;
private:
T *data;
unsigned int length;
public:
Array();
Array(unsigned int n);
Array(const Array &other);
Array &operator=(const Array &other);
~Array();
public:
Array();
Array(const Array &other);
Array& operator=(const Array &other);
~Array();
T &operator[](unsigned int i);
T const &operator[](unsigned int i) const;
unsigned int size() const;
Array(unsigned int n);
T &operator[](unsigned int n);
unsigned int size() const;
};
#include "Array.tpp"
# include "Array.tpp"
#endif

View File

@@ -1,73 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.tpp :+: :+: :+: */
/* Array.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/21 17:09:36 by aortigos #+# #+# */
/* Updated: 2026/03/22 21:42:21 by aortigos ### ########.fr */
/* 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), _size(0)
Array<T>::Array() : data(NULL), length(0)
{
// std::cout << "Default constructor called" << std::endl;
// std::cout << "Array default constructor called" << std::endl;
}
template <typename T>
Array<T>::Array(unsigned int n) : _size(n)
Array<T>::Array(const Array &other)
{
data = new T[n];
}
template <typename T>
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];
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)
{
delete[] data;
_size = other._size;
data = new T[_size];
for (unsigned int i = 0;i < _size; i++)
data[i] = other.data[i];
}
return (*this);
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;
delete[] data;
// std::cout << "Array destructor called" << std::endl;
}
template <typename T>
T &Array<T>::operator[](unsigned int i)
Array<T>::Array(unsigned int n) : length(n)
{
if (i >= _size)
throw std::exception();
return (data[i]);
data = new T[n]();
}
template <typename T>
T const &Array<T>::operator[](unsigned int i) const
T &Array<T>::operator[](unsigned int n)
{
if (i >= _size)
throw std::exception();
return (data[i]);
if (n >= length)
throw std::exception();
return (data[n]);
}
template <typename T>
unsigned int Array<T>::size() const
unsigned int Array<T>::size() const
{
return (_size);
}
return (this->length);
}