Starting ex02 from zero

This commit is contained in:
aortigos
2026-03-22 21:27:19 +01:00
parent 5c54cbee03
commit 5eae0db20d
2 changed files with 62 additions and 37 deletions

View File

@@ -16,6 +16,24 @@
# include <iostream> # include <iostream>
template <typename T>
class Array
{
private:
T *data;
unsigned int _size;
public:
Array();
Array(unsigned int n);
Array(const Array &other);
Array &operator=(const Array &other);
~Array();
T &operator[](unsigned int i);
unsigned int size() const;
};
#include "Array.tpp" #include "Array.tpp"
#endif #endif

View File

@@ -6,49 +6,56 @@
/* 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/21 17:11:51 by aortigos ### ########.fr */ /* Updated: 2026/03/22 14:52:48 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
// Construction with no parameter -> creates an empty array
// Construction with an unsigned int n as parameter
// Consctrucion by copy and assignment operator
// operator new [] to allocate memory
// Access elements by operator []
// Exception if access index out of bounds
// Function size
template <typename T> template <typename T>
class Array Array<T>::Array() : data(NULL), _size(0)
{ {
private: // std::cout << "Default constructor called" << std::endl;
T *element;
unsigned int size;
public:
Array()
{
new T[n]();
} }
template <typename T>
Array<T>::Array(unsigned int n)
{
}
template <typename T>
Array(const Array &other) Array(const Array &other)
{ {
if (this != other)
{
this->element = other.element;
this->size = other.size;
}
return (*this);
} }
template <typename T>
Array &operator=(const Array &other) Array &operator=(const Array &other)
{ {
this = other;
} }
template <typename T>
~Array() ~Array()
{ {
delete[] T;
} }
Array(unsigned int n) template <typename T>
T &operator[](unsigned int i)
{ {
this->size = n;
} }
int size() template <typename T>
unsigned int size() const
{ {
return (this->size);
} }
};