This commit is contained in:
2024-03-02 20:37:01 +01:00
parent c3a7af50b0
commit d36af81adb
13 changed files with 591 additions and 12 deletions

92
cpp07/ex02/Array.hpp Normal file
View File

@@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/02 20:05:14 by fgras-ca #+# #+# */
/* Updated: 2024/03/02 20:31:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include <cstddef> // Pour size_t
#include <stdexcept> // Pour std::out_of_range
#include <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
template<typename T>
class Array
{
private:
T* elements;
size_t n;
public:
// Constructeur par défaut
Array() : elements(new T[0]), n(0)
{
std::cout << GREEN << "Array default constructor called!" << RESET << std::endl;
}
// Constructeur avec taille
Array(unsigned int n) : elements(new T[n]), n(n)
{
std::cout << GREEN << "Array with size constructor called!" << RESET << std::endl;
}
// Constructeur de copie
Array(const Array& other) : elements(new T[other.n]), n(other.n)
{
for (size_t i = 0; i < n; ++i)
{
elements[i] = other.elements[i];
}
}
// Destructeur
~Array()
{
delete[] elements;
std::cout << RED << "Array " << static_cast<void*>(this->elements) << " is destroyed!" << RESET << std::endl;
}
// Opérateur d'assignation
Array& operator=(const Array& other)
{
if (this != &other)
{
delete[] elements;
n = other.n;
elements = new T[n];
for (size_t i = 0; i < n; ++i)
{
elements[i] = other.elements[i];
}
}
return (*this);
}
// Accès aux éléments
T& operator[](size_t index)
{
if (index >= n) throw std::out_of_range("Index out of bounds");
return (elements[index]);
}
// Taille du tableau
size_t size() const
{
return (n);
}
};
#endif

60
cpp07/ex02/Makefile Normal file
View File

@@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/03/02 20:05:50 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
DEF_COLOR = \033[0m
GRAY = \033[0;90m
RED = \033[0;91m
GREEN = \033[0;92m
YELLOW = \033[0;93m
BLUE = \033[0;94m
MAGENTA = \033[0;95m
CYAN = \033[0;96m
WHITE = \033[0;97m
ORANGE = \033[38;5;214m
NAME = array
SRC = main.cpp \
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./array" to execute the program!!$(DEF_COLOR)"
$(LOGO)
clean :
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
$(RM) $(OBJS)
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
$(LOGO)
fclean : clean
@echo "$(RED)Delete program name... $(DEF_COLOR)"
$(RM) $(NAME)
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
$(LOGO)
re : fclean all
.PHONY : all clean fclean re

43
cpp07/ex02/main.cpp Normal file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/02 20:15:43 by fgras-ca #+# #+# */
/* Updated: 2024/03/02 20:34:26 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Array.hpp"
int main()
{
Array<int> intArray(5);
for (size_t i = 0; i < intArray.size(); ++i)
{
intArray[i] = static_cast<int>(i * 2);
std::cout << BLUE << "intArray[" << i << "] = " << RESET << intArray[i] << std::endl;
}
// Test de copie
Array<int> copiedArray = intArray;
std::cout << GREEN << "copiedArray size: " << RESET << copiedArray.size() << std::endl;
// Modifier l'original ne devrait pas affecter la copie
intArray[0] = 100;
std::cout << YELLOW << "\nAfter modification: " << RESET << std::endl;
std::cout << "intArray[0] = " << intArray[0] << ", copiedArray[0] = " << copiedArray[0] << std::endl;
// Test d'accès hors limites
try
{
intArray[intArray.size()] = 10; // Devrait lancer une exception
}
catch (const std::out_of_range& e)
{
std::cout << RED << "\nException caught: " << RESET << e.what() << std::endl;
}
return (0);
}