From d36af81adb6f8bde447916d009e6443f316eaccc Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Sat, 2 Mar 2024 20:37:01 +0100 Subject: [PATCH] ex07 --- .vscode/settings.json | 50 ++++++++++++++++++- cpp05/ex02/AForm.cpp | 2 +- cpp05/ex02/Bureaucrat.hpp | 20 ++++---- cpp06/ex01/convert | Bin 18072 -> 0 bytes cpp07/ex00/Makefile | 60 +++++++++++++++++++++++ cpp07/ex00/main.cpp | 33 +++++++++++++ cpp07/ex00/whatever.hpp | 46 ++++++++++++++++++ cpp07/ex01/Makefile | 60 +++++++++++++++++++++++ cpp07/ex01/iter.hpp | 37 ++++++++++++++ cpp07/ex01/main.cpp | 100 ++++++++++++++++++++++++++++++++++++++ cpp07/ex02/Array.hpp | 92 +++++++++++++++++++++++++++++++++++ cpp07/ex02/Makefile | 60 +++++++++++++++++++++++ cpp07/ex02/main.cpp | 43 ++++++++++++++++ 13 files changed, 591 insertions(+), 12 deletions(-) delete mode 100755 cpp06/ex01/convert create mode 100644 cpp07/ex00/Makefile create mode 100644 cpp07/ex00/main.cpp create mode 100644 cpp07/ex00/whatever.hpp create mode 100644 cpp07/ex01/Makefile create mode 100644 cpp07/ex01/iter.hpp create mode 100644 cpp07/ex01/main.cpp create mode 100644 cpp07/ex02/Array.hpp create mode 100644 cpp07/ex02/Makefile create mode 100644 cpp07/ex02/main.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index cb996a9..32d6a19 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,53 @@ { "files.associations": { - "iostream": "cpp" + "iostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "cstring": "cpp" } } \ No newline at end of file diff --git a/cpp05/ex02/AForm.cpp b/cpp05/ex02/AForm.cpp index 0b9a6b3..8575f4b 100644 --- a/cpp05/ex02/AForm.cpp +++ b/cpp05/ex02/AForm.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ -/* Updated: 2024/02/22 14:36:36 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/02 20:36:08 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp index 794108d..522d103 100644 --- a/cpp05/ex02/Bureaucrat.hpp +++ b/cpp05/ex02/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ -/* Updated: 2024/02/22 15:29:48 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/02 19:04:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,15 @@ #include #include "AForm.hpp" -#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" +#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" class AForm; diff --git a/cpp06/ex01/convert b/cpp06/ex01/convert deleted file mode 100755 index 9d032219639182a2556723d584394b426b5af3eb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 18072 zcmeHPeQ;b?b-%l^ut;QU{go(oYcI~nIM`ZiCHW%`j#rjeStYWqXf0z*%wx6tEbU~q ztL(mwr3|40Cz+^@rw&ZP6gnuwOcGiKObdagokqdI0UArqeBAraz3;xackl5ZMSHsZKA+%HBW@GKO)gVNL<>&c zrYj&3v0W^|-=J70-UWV<#FRXu2&AgK;xDIl8m|E*yGE*1pnsrX!IXPQlPQT`g5L@1`G+LLr#4+-@&O#wbDg*QTG>{b%a5qu4YzDgEZ1<#bxdrB}Dhb_A1M zOxwk@9n*leW6JTQn9ygt?vI-`Lq>(E+ipnPxoM{|6HKWMD)HB>)RnZ?tLGr(F;hc^qQyOn7n=|Qwt!8t3@Ha@M?CuZV-Nkm@bDk?(0|6mKaEciA9s1&!w=k4 z)ZgTRZ}4dMH2kl3Hwu0o^p}Y>;=}Dq*a)7=r=d@HBqCW6(6}&+!QouiC|HTSWf;Qf z-oM94nR#=%$6|B6O81CM0>~A)r@oh%moqOZfHp57cj)ueG7NrHHbat>i-e$zN82iIU zG%Ack-g=%5cZ8$&!7^^Onb}mPQU~9rZ(9)#n6@Unw|hF*rJEjF|aCE}O`ttuf=iX4OrP zV0s2A5s&uW3D>gk7{-O8SSd)!{TRM2M(>@;O2N^A_r4U*-srGm$db9DC5-;;(R4Ot z^qGa?FykpVyxVC_|TeNBXM$EUz!UH;r8_pm~jc zp!lF&k2=XC_^eE)m(UYnt;W9g*r)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x7lJQHkaB%N*1G;u{Q9CrLd7ahhN87LIEVVaudnNV(z&|f&+ zB92^{`DvvGr0kx%#r{z-+N-DA+vlZ~r3ghhdB=54=%l)n5pPQAa)!Wt{c5sT$c*#I ze&d_OP6=}b$bDxD5h{!gTZsWsE3fDf*YHEAnI91${E}&g2D8P`NIr)&FKf&J4HVNz zMNOwPgdC(Mdelkvp+sRwgi>Q!G@+=KSCadXEQUu%8Dl_}H!}$`(6y0_B|>tZhENR+ z=D<>=U?xQfiB(|b$`Q)vnWIT_#KMD^G6Rs@6M~fffY5yYh+oW@2O6E{+`SPiy<#+9B_I zG39+SvZwFxZu`f9QB2rBUd2_>l=lNQx?uc22Z7T*sO^|ev7x3e_u@&H{dujxl=ayS z^Gr{>>@R8oQ<6Ii+cEtc2%Y`s?~_cwNQ34mY{!t=SDB9=f1hT``>m|+zJISHJANIXhB0^ zs#yi%FSX??{|RO(J{)G=hvxrB!5wdIpJ@#o(w+d@^FDQ?NwvayikqMRj5olZ=04l= zz7zky7|!*Fe6St&>o0-PHL*SKQy<(++Cn^~i2axCnNl9T(_TchYO|tzpR)tDV}31+ z3FG!75hZEV_7T_kX|Ynm-|KbzTHVIkexr@33iXwWle(Ia@|bowsuvd8SEW52?mu4_ o?ay-GrJ?>aMGrQVC+&qv@!YQBXpf+>{h-bZrZYay;$p>r1N<>m%m4rY diff --git a/cpp07/ex00/Makefile b/cpp07/ex00/Makefile new file mode 100644 index 0000000..208c3bc --- /dev/null +++ b/cpp07/ex00/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/02 19:20:09 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 = whatever + +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 "./whatever" 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 \ No newline at end of file diff --git a/cpp07/ex00/main.cpp b/cpp07/ex00/main.cpp new file mode 100644 index 0000000..5d3821d --- /dev/null +++ b/cpp07/ex00/main.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 19:06:16 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 19:13:43 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "whatever.hpp" + +int main() +{ + int a = 2; + int b = 3; + ::swap(a, b); + std::cout << RED << "a = " << RESET << a << RED << ", b = " << RESET << b << std::endl; + std::cout << GREEN << "min( a, b ) = " << RESET << ::min(a, b) << std::endl; + std::cout << MAGENTA << "max( a, b ) = " << RESET << ::max(a, b) << std::endl; + + std::string c = "chaine1"; + std::string d = "chaine2"; + ::swap(c, d); + std::cout << RED << "c = " << RESET << c << RED << ", d = " << RESET << d << std::endl; + std::cout << GREEN << "min( c, d ) = " << RESET << ::min(c, d) << std::endl; + std::cout << MAGENTA << "max( c, d ) = " << RESET << ::max(c, d) << std::endl; + + return (0); +} \ No newline at end of file diff --git a/cpp07/ex00/whatever.hpp b/cpp07/ex00/whatever.hpp new file mode 100644 index 0000000..c9a346a --- /dev/null +++ b/cpp07/ex00/whatever.hpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whatever.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 18:56:24 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 19:05:10 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WHATEVER_HPP +#define WHATEVER_HPP + +#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 +void swap(T& a, T& b) +{ + T temp = a; + a = b; + b = temp; +} + +template +T const &min(T const &a, T const &b) +{ + return (a < b) ? a : b; +} + +template +T const &max(T const &a, T const &b) +{ + return (a > b) ? a : b; +} + +#endif \ No newline at end of file diff --git a/cpp07/ex01/Makefile b/cpp07/ex01/Makefile new file mode 100644 index 0000000..8c5a96d --- /dev/null +++ b/cpp07/ex01/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/02 20:03:32 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 = iter + +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 "./iter" 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 \ No newline at end of file diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp new file mode 100644 index 0000000..c4edbac --- /dev/null +++ b/cpp07/ex01/iter.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 19:21:08 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 19:56:14 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITER_HPP +#define ITER_HPP + +#include + +#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 +void iter(T* array, size_t length, void (*func)(T& element)) +{ + for (size_t i = 0; i < length; ++i) + { + func(array[i]); + } +} + +#endif \ No newline at end of file diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp new file mode 100644 index 0000000..bcbc73b --- /dev/null +++ b/cpp07/ex01/main.cpp @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 19:26:15 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 20:03:13 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "iter.hpp" +#include +#include // Pour std::toupper +#include // Pour la fonction std::strlen et std::strcpy + +// Fonction pour doubler les valeurs +void doubleValue(int& value) +{ + value *= 2; +} + +// Fonction pour convertir une chaîne en majuscules +void toUpperCase(std::string& str) +{ + for (size_t i = 0; i < str.size(); ++i) + { + str[i] = std::toupper(str[i]); + } +} + +// Un objet personnalisé pour démontrer l'utilisation avec iter +class Person +{ +public: + std::string name; + int age; + + // Constructeur par défaut + Person() : name(""), age(0) {} + + // Constructeur paramétrique + Person(std::string n, int a) : name(n), age(a) {} + + // Destructeur + ~Person() {} + + // Constructeur de copie + Person(const Person& other) : name(other.name), age(other.age) {} + + // Opérateur d'affectation + Person& operator=(const Person& other) + { + if (this != &other) // Protection contre l'auto-affectation + { + name = other.name; + age = other.age; + } + return (*this); + } +}; + +// Fonction pour afficher les attributs d'un objet Person +void displayPerson(Person& person) +{ + std::cout << person.name << ", " << person.age << " years old" << std::endl; +} + +int main() +{ + int nums[] = {1, 2, 3, 4, 5}; + std::string strings[] = {"hello", "world", "iter", "function", "test"}; + Person people[3] = + { + Person("Alice", 30), + Person("Bob", 25), + Person("Charlie", 35) + }; + + std::cout << YELLOW << "Original nums: " << RESET; + for (int i = 0; i < 5; ++i) std::cout << nums[i] << " "; + std::cout << std::endl; + + iter(nums, 5, doubleValue); + + std::cout << YELLOW << "\nDoubled nums: " << RESET; + for (int i = 0; i < 5; ++i) std::cout << nums[i] << " "; + std::cout << std::endl; + + iter(strings, 5, toUpperCase); + + std::cout << MAGENTA << "\nStrings to upper case:" << RESET << std::endl; + for (int i = 0; i < 5; ++i) std::cout << strings[i] << std::endl; + + std::cout << RED << "\nDisplay people:" << RESET << std::endl; + iter(people, 3, displayPerson); + + return (0); +} diff --git a/cpp07/ex02/Array.hpp b/cpp07/ex02/Array.hpp new file mode 100644 index 0000000..58bfba8 --- /dev/null +++ b/cpp07/ex02/Array.hpp @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 // Pour size_t +#include // Pour std::out_of_range +#include + +#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 +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(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 diff --git a/cpp07/ex02/Makefile b/cpp07/ex02/Makefile new file mode 100644 index 0000000..1236580 --- /dev/null +++ b/cpp07/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 \ No newline at end of file diff --git a/cpp07/ex02/main.cpp b/cpp07/ex02/main.cpp new file mode 100644 index 0000000..bfe49a2 --- /dev/null +++ b/cpp07/ex02/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 intArray(5); + for (size_t i = 0; i < intArray.size(); ++i) + { + intArray[i] = static_cast(i * 2); + std::cout << BLUE << "intArray[" << i << "] = " << RESET << intArray[i] << std::endl; + } + + // Test de copie + Array 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); +}