From 62a2eb5e90ef48b8e41d852efc84c23e73644e42 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 26 Mar 2025 14:06:49 +0100 Subject: [PATCH] feat: finishing the ex02 of the module 06 --- cpp06/ex02/Makefile | 126 ++++++++++++++++++++++++++++++++++ cpp06/ex02/includes/A.hpp | 22 ++++++ cpp06/ex02/includes/B.hpp | 22 ++++++ cpp06/ex02/includes/Base.hpp | 22 ++++++ cpp06/ex02/includes/C.hpp | 22 ++++++ cpp06/ex02/includes/color.hpp | 47 +++++++++++++ cpp06/ex02/main.cpp | 104 ++++++++++++++++++++++++++++ 7 files changed, 365 insertions(+) create mode 100644 cpp06/ex02/Makefile create mode 100644 cpp06/ex02/includes/A.hpp create mode 100644 cpp06/ex02/includes/B.hpp create mode 100644 cpp06/ex02/includes/Base.hpp create mode 100644 cpp06/ex02/includes/C.hpp create mode 100644 cpp06/ex02/includes/color.hpp create mode 100644 cpp06/ex02/main.cpp diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile new file mode 100644 index 0000000..46995cb --- /dev/null +++ b/cpp06/ex02/Makefile @@ -0,0 +1,126 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2025/03/26 12:39:59 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = indentify + +# Commands +CXX = c++ +RM = rm -rf + +# Flags +# Mandatory flags for 42 +CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ +# Flags to have the dependences (can be removed for correction) +CXXFLAGS += -MMD +# Flags to have the debug (can be removed for correction) +DEBUG = -g3 +# DEBUG += -fsanitize=address +CXXFLAGS += $(DEBUG) + +# Sources +SRC = main.cpp + +# Objects +OBJDIRNAME = ./build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: header $(OBJ) footer + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.cpp + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @$(CXX) $(CXXFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +clangd: + @echo -e \ + "CompileFlags:\n" \ + " Add:\n" \ + " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ + " - \"-I"$(shell pwd)"/includes\"\n" \ + " - \"-xc++\"\n" \ + > .clangd + +# Phony +.PHONY: all bonus clean fclean re clangd +-include ${OBJ:.o=.d} diff --git a/cpp06/ex02/includes/A.hpp b/cpp06/ex02/includes/A.hpp new file mode 100644 index 0000000..02feca8 --- /dev/null +++ b/cpp06/ex02/includes/A.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */ +/* Updated: 2025/03/26 12:44:49 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Base.hpp" + +class A : public Base { + public: + virtual ~A() { + std::cout << "A destructor" << std::endl; + } +}; diff --git a/cpp06/ex02/includes/B.hpp b/cpp06/ex02/includes/B.hpp new file mode 100644 index 0000000..dd34617 --- /dev/null +++ b/cpp06/ex02/includes/B.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */ +/* Updated: 2025/03/26 12:45:08 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Base.hpp" + +class B : public Base { + public: + virtual ~B() { + std::cout << "B destructor" << std::endl; + } +}; diff --git a/cpp06/ex02/includes/Base.hpp b/cpp06/ex02/includes/Base.hpp new file mode 100644 index 0000000..8634cc3 --- /dev/null +++ b/cpp06/ex02/includes/Base.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */ +/* Updated: 2025/03/26 12:44:36 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Base { + public: + virtual ~Base() { + std::cout << "Base destructor" << std::endl; + }; +}; diff --git a/cpp06/ex02/includes/C.hpp b/cpp06/ex02/includes/C.hpp new file mode 100644 index 0000000..7130052 --- /dev/null +++ b/cpp06/ex02/includes/C.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */ +/* Updated: 2025/03/26 12:45:21 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Base.hpp" + +class C : public Base { + public: + virtual ~C() { + std::cout << "C destructor" << std::endl; + } +}; diff --git a/cpp06/ex02/includes/color.hpp b/cpp06/ex02/includes/color.hpp new file mode 100644 index 0000000..a52f798 --- /dev/null +++ b/cpp06/ex02/includes/color.hpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */ +/* Updated: 2025/03/24 13:58:15 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLOR_HPP +#define COLOR_HPP + +#define CLR_RESET "\033[0m" + +#define CLR_BLACK "\033[0;30m" +#define CLR_RED "\033[0;31m" +#define CLR_GREEN "\033[0;32m" +#define CLR_YELLOW "\033[0;33m" +#define CLR_BLUE "\033[0;34m" +#define CLR_MAGENTA "\033[0;35m" +#define CLR_CYAN "\033[0;36m" +#define CLR_WHITE "\033[0;37m" +#define CLR_GOLD "\033[38;5;220m" +#define CLR_GREY "\033[38;5;240m" + +#define CLR_LIGHT_BLACK "\033[0;90m" +#define CLR_LIGHT_RED "\033[0;91m" +#define CLR_LIGHT_GREEN "\033[0;92m" +#define CLR_LIGHT_YELLOW "\033[0;93m" +#define CLR_LIGHT_BLUE "\033[0;94m" +#define CLR_LIGHT_MAGENTA "\033[0;95m" +#define CLR_LIGHT_CYAN "\033[0;96m" +#define CLR_LIGHT_WHITE "\033[0;97m" + +#define CLR_BOLD_BLACK "\033[1;30m" +#define CLR_BOLD_RED "\033[1;31m" +#define CLR_BOLD_GREEN "\033[1;32m" +#define CLR_BOLD_YELLOW "\033[1;33m" +#define CLR_BOLD_BLUE "\033[1;34m" +#define CLR_BOLD_MAGENTA "\033[1;35m" +#define CLR_BOLD_CYAN "\033[1;36m" +#define CLR_BOLD_WHITE "\033[1;37m" + +#endif diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp new file mode 100644 index 0000000..e0695d7 --- /dev/null +++ b/cpp06/ex02/main.cpp @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/26 11:58:38 by rparodi #+# #+# */ +/* Updated: 2025/03/26 14:04:34 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +#include +#include +#include +#include +#include "color.hpp" + +Base* generate(void) { + std::srand(std::time(NULL)); + int choice = std::rand(); + Base *ptr; + switch (choice % 3) + { + case 0: + { + std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and A it's chosen ]" << CLR_RESET << std::endl << std::endl; + ptr = new A; + break; + } + case 1: + { + std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and B it's chosen ]" << CLR_RESET << std::endl << std::endl; + ptr = new B; + break; + } + case 2: + { + std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and C it's chosen ]" << CLR_RESET << std::endl << std::endl; + ptr = new C; + break; + } + default: + return NULL; + } + return ptr; +} + +/** + * @brief Indentify the type of the class + * + * @param ptr pointer to the class + */ +void identify(Base *ptr) { + if (dynamic_cast(ptr)) + std::cout << CLR_BLUE << "Indentify with pointer function:\t" << CLR_GOLD << "A" << CLR_RESET << std::endl; + else if (dynamic_cast(ptr)) + std::cout << CLR_BLUE << "Indentify with pointer function:\t" << CLR_GOLD << "B" << CLR_RESET << std::endl; + else if (dynamic_cast(ptr)) + std::cout << CLR_BLUE << "Indentify with pointer function:\t" << CLR_GOLD << "C" << CLR_RESET << std::endl; +} + +/** + * @brief Indentify the type of the class + * + * @param ref reference to the class + */ +void identify(Base &ref) { + try { + A &a = dynamic_cast(ref); + (void)a; + std::cout << CLR_BLUE << "Indentify with reference function:\t" << CLR_GOLD << "A" << CLR_RESET << std::endl; + } catch (std::bad_cast &e) {} + try { + B &b = dynamic_cast(ref); + (void)b; + std::cout << CLR_BLUE << "Indentify with reference function:\t" << CLR_GOLD << "B" << CLR_RESET << std::endl; + } catch (std::bad_cast &e) {} + try { + C &c = dynamic_cast(ref); + (void)c; + std::cout << CLR_BLUE << "Indentify with reference function:\t" << CLR_GOLD << "C" << CLR_RESET << std::endl; + } catch (std::bad_cast &e) {} +} + +int main(void) +{ + Base *test = generate(); + if (test == NULL) { + std::cerr << CLR_RED << "Error: generate() return nullptr" << CLR_RESET << std::endl; + return 1; + } + else { + identify(test); + identify(*test); + std::cout << std::endl; + delete test; + return 0; + } +}