Bladeren bron

added modules 07 templates

bastien 3 weken geleden
bovenliggende
commit
362dcfd6a9

+ 50 - 0
Module_07/ex00/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/09 14:18:13 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 39 - 0
Module_07/ex00/includes/Swap.hpp

@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Swap.hpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/09 14:08:45 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/09 15:26:46 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+
+#ifndef SWAP_HPP
+# define SWAP_HPP
+
+# include <iostream>
+# include <string>
+
+template < typename T >
+void    swap(T &x, T &y) {
+    T   temp;
+
+    temp = x;
+    x = y;
+    y = temp;
+}
+
+template < typename T >
+T    min(T const & x, T const & y) {
+    return (x < y ? x : y);
+}
+
+template < typename T >
+T    max(T const & x, T const & y) {
+    return (x > y ? x : y);
+}
+
+#endif

+ 30 - 0
Module_07/ex00/srcs/main.cpp

@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/09 14:15:06 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/09 14:23:11 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "Swap.hpp"
+
+int main( void ) {
+    int a = 2;
+    int b = 3;
+    
+    ::swap( a, b );
+    std::cout << "a = " << a << ", b = " << b << std::endl;
+    std::cout << "min( a, b ) = " << ::min( a, b ) << std::endl;
+    std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl;
+    std::string c = "chaine1";
+    std::string d = "chaine2";
+    ::swap(c, d);
+    std::cout << "c = " << c << ", d = " << d << std::endl;
+    std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl;
+    std::cout << "max( c, d ) = " << ::max( c, d ) << std::endl;
+    return 0;
+}

+ 50 - 0
Module_07/ex01/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/09 14:18:13 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 62 - 0
Module_07/ex01/includes/iter.hpp

@@ -0,0 +1,62 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   iter.hpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/09 14:24:10 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/09 18:42:40 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef ITER_HPP
+# define ITER_HPP
+
+# include <iostream>
+# include <string>
+
+template < typename T >
+void    iter(T const *array, size_t size, void (*fn)(T const &)) {
+    size_t  i;
+
+    i = 0;
+    while (i < size)
+        fn(array[i++]);
+}
+
+template < typename T >
+void    iter(T const *array, size_t size, void (*fn)(T &)) {
+    size_t  i;
+    T   *tmp;
+
+    tmp = const_cast<T*>(array);
+    i = 0;
+    while (i < size)
+        fn(tmp[i++]);
+}
+
+template < typename T >
+void    iter(T *array, size_t size, void (*fn)(T &)) {
+    size_t  i;
+
+    i = 0;
+    while (i < size)
+        fn(array[i++]);
+}
+
+void    print(int & value) {
+    std::cout << "print " << value << std::endl;
+}
+
+template < typename T >
+void    printConst(T const & value) {
+    std::cout << "print " << value << std::endl;
+}
+
+template < typename T >
+void    addTen(T & a) {
+    a += 10;
+}
+
+#endif

+ 44 - 0
Module_07/ex01/srcs/main.cpp

@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/09 14:15:06 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/09 18:43:02 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "iter.hpp"
+
+class Test {
+    public:
+        Test(void) : _value(42) {return;};
+        int         getValue(void) const {return this->_value;};
+
+    private:
+        int         _value;
+};
+
+std::ostream &			operator<<( std::ostream & o, Test const & i ) {
+    o << i.getValue();
+    return o;
+}
+
+int main( void ) {
+    int arr1[] = {1, 2, 3};
+    int const arr2[] = {4, 5, 6};
+    int arr3[] = {1, 2, 3, 4, 5};
+    Test tab[5];
+    
+    iter(arr3, 5, printConst);
+    iter(tab, 5, printConst);
+
+    iter(arr1, 3, addTen);
+    std::cout << arr1[0] << "; " << arr1[1] << "; " << arr1[2] << std::endl;
+    iter(arr1, 3, printConst);
+
+    iter(arr2, 3, printConst);
+    iter(arr2, 3, print);
+}

+ 50 - 0
Module_07/ex02/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/11 16:26:46 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = a.out
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main 
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

+ 77 - 0
Module_07/ex02/includes/Array.hpp

@@ -0,0 +1,77 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Array.hpp                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2026/01/11 15:29:11 by bchanot           #+#    #+#             */
+/*   Updated: 2026/01/11 16:52:20 by bchanot          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef ARRAY_HPP
+# define ARRAY_HPP
+
+# include <iostream>
+# include <string>
+
+template <typename T>
+class Array
+{
+
+	public:
+
+		Array() { this->_elements = new T[]; };
+		Array(unsigned int const n) : _size(n), _elements(new T[n]) {};
+		Array( Array const & src ) : _size(src.size()), _elements(new T[this->_size]) { 
+			size_t	fill = 0;
+
+			while (fill < this->_size) {
+				this->_elements[fill] = src[fill];
+				fill++;
+			}
+		};
+		~Array() { delete[] this->_elements; };
+
+		class IndexOutOfBounds : public std::exception {
+			public:
+				virtual const char *what() const throw() {
+					return ("The index specified is out of bounds !");
+				}
+		};
+
+		Array &		operator=( Array const & rhs ) { 
+			if (rhs == *this)
+				return *this;
+			size_t	fill = 0;
+
+			this->_size = rhs.size();
+			this->_elements = new T[this->_size];
+
+			while (fill < this->_size) {
+				this->_elements[fill] = rhs[fill];
+				fill++;
+			}
+			return *this;
+		};
+		T			&operator[](size_t idx) { 
+			if (idx >= this->_size)
+				throw IndexOutOfBounds();
+			return this->_elements[idx];
+		};
+		T const 	&operator[](size_t idx) const { 
+			if (idx >= this->_size)
+				throw IndexOutOfBounds();
+			return this->_elements[idx];
+		};
+
+		size_t		size(void) const { return this->_size; };
+
+	private:
+		size_t	_size;
+		T		*_elements;
+
+};
+
+#endif /* *********************************************************** ARRAY_H */

+ 0 - 0
Module_07/main.cpp → Module_07/ex02/srcs/main.cpp