From c3a7af50b050516076dcbe584acdff52179c4196 Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Sat, 2 Mar 2024 17:39:42 +0100 Subject: [PATCH] ex06 --- .vscode/settings.json | 5 +++ cpp05/ex01/Form.hpp | 26 ++++++++---- cpp05/ex01/Makefile | 2 +- cpp06/ex00/ScalarConverter.cpp | 74 ++++++++++++++++++++------------- cpp06/ex01/Data.hpp | 24 +++++++++++ cpp06/ex01/Makefile | 61 +++++++++++++++++++++++++++ cpp06/ex01/Serializer.cpp | 42 +++++++++++++++++++ cpp06/ex01/Serializer.hpp | 47 +++++++++++++++++++++ cpp06/ex01/convert | Bin 0 -> 18072 bytes cpp06/ex01/main.cpp | 54 ++++++++++++++++++++++++ cpp06/ex02/A.hpp | 21 ++++++++++ cpp06/ex02/B.hpp | 21 ++++++++++ cpp06/ex02/Base.hpp | 22 ++++++++++ cpp06/ex02/C.hpp | 21 ++++++++++ cpp06/ex02/Makefile | 61 +++++++++++++++++++++++++++ cpp06/ex02/Utils.cpp | 52 +++++++++++++++++++++++ cpp06/ex02/Utils.hpp | 38 +++++++++++++++++ cpp06/ex02/main.cpp | 43 +++++++++++++++++++ 18 files changed, 575 insertions(+), 39 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 cpp06/ex01/Data.hpp create mode 100644 cpp06/ex01/Makefile create mode 100644 cpp06/ex01/Serializer.cpp create mode 100644 cpp06/ex01/Serializer.hpp create mode 100755 cpp06/ex01/convert create mode 100644 cpp06/ex01/main.cpp create mode 100644 cpp06/ex02/A.hpp create mode 100644 cpp06/ex02/B.hpp create mode 100644 cpp06/ex02/Base.hpp create mode 100644 cpp06/ex02/C.hpp create mode 100644 cpp06/ex02/Makefile create mode 100644 cpp06/ex02/Utils.cpp create mode 100644 cpp06/ex02/Utils.hpp create mode 100644 cpp06/ex02/main.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cb996a9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp index c3ee743..4f35505 100644 --- a/cpp05/ex01/Form.hpp +++ b/cpp05/ex01/Form.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ -/* Updated: 2024/02/22 12:48:27 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/26 15:57:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,23 +19,25 @@ class Bureaucrat; -class Form +class AForm { private: const std::string name; + const bool isSigned; const int gradeRequiredToSign; const int gradeRequiredToExecute; public: //constructueur - Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); + AForm(const::std::string& name); + AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); // Constructeur par copie - Form(const Form& other); + AForm(const AForm& other); // Opérateur d'affectation - Form& operator=(const Form& other); + AForm& operator=(const AForm& other); // Destructeur - ~Form(); + ~AForm(); std::string getName() const; @@ -48,17 +50,23 @@ public: class GradeTooHighException : public std::exception { public: - const char* what() const throw(); + const char* what() const noexcept override + { + return ("Grade too high"); + } }; class GradeTooLowException : public std::exception { public: - const char* what() const throw(); + const char* what() const noexcept override + { + return ("Grade too low"); + } }; }; // Surcharge de l'opérateur d'insertion -std::ostream& operator<<(std::ostream& os, const Form& form); +std::ostream& operator<<(std::ostream& os, const AForm& form); #endif diff --git a/cpp05/ex01/Makefile b/cpp05/ex01/Makefile index ca43fe0..8e6b0ac 100644 --- a/cpp05/ex01/Makefile +++ b/cpp05/ex01/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # -# Updated: 2024/02/22 12:41:21 by fgras-ca ### ########.fr # +# Updated: 2024/02/26 14:35:12 by fgras-ca ### ########.fr # # # # **************************************************************************** # diff --git a/cpp06/ex00/ScalarConverter.cpp b/cpp06/ex00/ScalarConverter.cpp index 9aae9ea..d62fa17 100644 --- a/cpp06/ex00/ScalarConverter.cpp +++ b/cpp06/ex00/ScalarConverter.cpp @@ -6,21 +6,17 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/25 19:33:13 by fgras-ca #+# #+# */ -/* Updated: 2024/02/25 20:06:27 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/26 16:03:04 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" ScalarConverter::ScalarConverter() -{ - std::cout << GREEN << "ScalarConverter default constructor called!" << RESET << std::endl; -} +{} ScalarConverter::ScalarConverter(const ScalarConverter&) -{ - std::cout << CYAN << "Copy constructor called for ScalarConverter." << RESET << std::endl; -} +{} ScalarConverter& ScalarConverter::operator=(const ScalarConverter&) { @@ -28,9 +24,7 @@ ScalarConverter& ScalarConverter::operator=(const ScalarConverter&) } ScalarConverter::~ScalarConverter() -{ - std::cout << RED << "ScalarConverter is destroyed!" << RESET << std::endl; -} +{} char ScalarConverter::convertToChar(double num) { @@ -60,31 +54,53 @@ double ScalarConverter::convertToDouble(const std::string& literal) void ScalarConverter::convert(const std::string& literal) { - double num = convertToDouble(literal); - try + // Traitement spécial si la chaîne représente un caractère unique + if (literal.length() == 1 && !isdigit(literal[0])) { - char c = convertToChar(num); + char c = literal[0]; printAsChar(c); - } - catch (const std::exception& e) - { - std::cout << "char: " << e.what() << std::endl; - } + // Convertir directement le caractère en numérique pour les autres types + double num = static_cast(c); + try + { + printAsInt(static_cast(c)); + } + catch (const std::exception& e) + { + std::cout << "int: " << e.what() << std::endl; + } - try - { - int i = convertToInt(num); - printAsInt(i); + printAsFloat(static_cast(num)); + printAsDouble(num); } - catch (const std::exception& e) + else { - std::cout << "int: " << e.what() << std::endl; + // L'approche originale pour les entrées considérées comme numériques + double num = convertToDouble(literal); + try + { + char c = convertToChar(num); + printAsChar(c); + } + catch (const std::exception& e) + { + std::cout << "char: " << e.what() << std::endl; + } + + try + { + int i = convertToInt(num); + printAsInt(i); + } + catch (const std::exception& e) + { + std::cout << "int: " << e.what() << std::endl; + } + + float f = convertToFloat(num); + printAsFloat(f); + printAsDouble(num); } - - float f = convertToFloat(num); - printAsFloat(f); - - printAsDouble(num); } void ScalarConverter::printAsChar(char c) diff --git a/cpp06/ex01/Data.hpp b/cpp06/ex01/Data.hpp new file mode 100644 index 0000000..54d93d5 --- /dev/null +++ b/cpp06/ex01/Data.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Data.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/26 15:59:05 by fgras-ca #+# #+# */ +/* Updated: 2024/02/26 16:11:24 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DATA_HPP +# define DATA_HPP + +# include + +typedef struct s_data +{ + std::string dummy_str; +} Data; + +#endif + diff --git a/cpp06/ex01/Makefile b/cpp06/ex01/Makefile new file mode 100644 index 0000000..5de1431 --- /dev/null +++ b/cpp06/ex01/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/02/26 16:28:12 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 = serializer + +SRC = Serializer.cpp \ + 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 "./serializer" 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/cpp06/ex01/Serializer.cpp b/cpp06/ex01/Serializer.cpp new file mode 100644 index 0000000..65f7c71 --- /dev/null +++ b/cpp06/ex01/Serializer.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Serializer.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/26 16:00:38 by fgras-ca #+# #+# */ +/* Updated: 2024/02/26 16:04:34 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Serializer.hpp" + +// Constructeur privé +Serializer::Serializer() +{} +// Constructeur de copie privé +Serializer::Serializer(const Serializer&) +{ + // Généralement vide car la classe ne doit pas être copiée. +} +// Opérateur d'affectation privé +Serializer& Serializer::operator=(const Serializer&) +{ // Généralement vide car l'affectation ne doit pas être effectuée. + return (*this); +} +// Destructeur privé +Serializer::~Serializer() +{ + // Généralement vide car la classe ne doit pas être instanciée. +} + +uintptr_t Serializer::serialize(Data* ptr) +{ + return (reinterpret_cast(ptr)); +} + +Data* Serializer::deserialize(uintptr_t raw) +{ + return (reinterpret_cast(raw)); +} \ No newline at end of file diff --git a/cpp06/ex01/Serializer.hpp b/cpp06/ex01/Serializer.hpp new file mode 100644 index 0000000..57141fe --- /dev/null +++ b/cpp06/ex01/Serializer.hpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Serializer.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/26 15:58:04 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 13:41:15 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERIALIZER_HPP +#define SERIALIZER_HPP + +#include "Data.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" + +class Serializer +{ +public: + // Méthodes statiques publiques pour la sérialisation et la désérialisation + static uintptr_t serialize(Data* ptr); + static Data* deserialize(uintptr_t raw); + +private: + // Constructeur par défaut privé pour empêcher l'instanciation + Serializer(); + // Constructeur de copie privé pour empêcher la copie + Serializer(const Serializer&); + // Opérateur d'affectation privé pour empêcher l'affectation + Serializer& operator=(const Serializer&); + // Destructeur privé + ~Serializer(); +}; + +#endif diff --git a/cpp06/ex01/convert b/cpp06/ex01/convert new file mode 100755 index 0000000000000000000000000000000000000000..9d032219639182a2556723d584394b426b5af3eb GIT binary patch literal 18072 zcmeHPeQ;b?b-%l^ut;QU{go(oYcI~nIM`ZiCHW%`j#rjeStYWqXf0z*%wx6tEbU~q ztL(mwr3|40Cz+^@rw&ZP6gnuwOcGiKObdagokqdI0UArqe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`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 literal 0 HcmV?d00001 diff --git a/cpp06/ex01/main.cpp b/cpp06/ex01/main.cpp new file mode 100644 index 0000000..24472f9 --- /dev/null +++ b/cpp06/ex01/main.cpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/26 16:06:41 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 13:46:46 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Serializer.hpp" +#include +#include + +int main() +{ + std::cout << YELLOW << "Enter a string to be stored in Data: " << RESET; + std::string input; + std::getline(std::cin, input); // Capture l'entrée utilisateur, incluant les espaces. + + Data data; // Création d'une instance de Data. + data.dummy_str = input; // Stocke la chaîne saisie dans l'instance de Data. + std::cout << CYAN << "Original data content: " << RESET << data.dummy_str << std::endl; + + // Sérialisation de l'adresse de l'instance de Data. + uintptr_t serialized = Serializer::serialize(&data); + std::cout << CYAN << "Serialized value (address as integer): " << RESET << serialized << std::endl; + + // Désérialisation de la valeur sérialisée pour obtenir le pointeur vers l'instance de Data. + Data* deserialized = Serializer::deserialize(serialized); + std::cout << CYAN << "Deserialized data content: " << RESET << deserialized->dummy_str << std::endl; + + // Vérification de la cohérence entre l'instance originale et l'instance désérialisée. + if (&data == deserialized) + { + std::cout << YELLOW << "Verification: Original and deserialized data point to the same instance." << RESET << std::endl; + } + else + { + std::cout << RED << "Error: Original and deserialized data do not point to the same instance." << RESET << std::endl; + } + + // Logs de l'entrée standard à la fin du programme + std::cout << MAGENTA << "Final Log:" << RESET << std::endl; + std::cout << CYAN << "- Original input: " << RESET << input << std::endl; + std::cout << CYAN << "- Serialized value: " << RESET << serialized << std::endl; + std::cout << CYAN << "- Deserialized content: " << RESET << deserialized->dummy_str << std::endl; + std::cout << CYAN << "- Original and deserialized comparison: " << RESET << (&data == deserialized ? "Identical" : "Different") << std::endl; + + return (0); +} + diff --git a/cpp06/ex02/A.hpp b/cpp06/ex02/A.hpp new file mode 100644 index 0000000..cb80fea --- /dev/null +++ b/cpp06/ex02/A.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:24:33 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 14:48:02 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef A_HPP +#define A_HPP + +#include "Base.hpp" + +class A: public Base +{}; + +#endif \ No newline at end of file diff --git a/cpp06/ex02/B.hpp b/cpp06/ex02/B.hpp new file mode 100644 index 0000000..7352668 --- /dev/null +++ b/cpp06/ex02/B.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:26:30 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 14:48:12 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_HPP +#define B_HPP + +#include "Base.hpp" + +class B: public Base +{}; + +#endif \ No newline at end of file diff --git a/cpp06/ex02/Base.hpp b/cpp06/ex02/Base.hpp new file mode 100644 index 0000000..13fd0cb --- /dev/null +++ b/cpp06/ex02/Base.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:23:13 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 15:02:16 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BASE_HPP +#define BASE_HPP + +class Base +{ +public: + virtual ~Base() {}; +}; + +#endif \ No newline at end of file diff --git a/cpp06/ex02/C.hpp b/cpp06/ex02/C.hpp new file mode 100644 index 0000000..19d0f33 --- /dev/null +++ b/cpp06/ex02/C.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:27:43 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 14:48:28 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_HPP +#define C_HPP + +#include "Base.hpp" + +class C: public Base +{}; + +#endif \ No newline at end of file diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile new file mode 100644 index 0000000..252860d --- /dev/null +++ b/cpp06/ex02/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/02/27 14:47:25 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 = identify + +SRC = Utils.cpp \ + 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 "./identify" 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/cpp06/ex02/Utils.cpp b/cpp06/ex02/Utils.cpp new file mode 100644 index 0000000..90d91b9 --- /dev/null +++ b/cpp06/ex02/Utils.cpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Utils.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:33:46 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 15:07:41 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Utils.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" +#include +#include +#include + +Base* generate(void) +{ + srand(static_cast(time(NULL))); + int choice = rand() % 3; + switch (choice) + { + case 0: return new A(); + case 1: return new B(); + case 2: return new C(); + } + return (NULL); // Par défaut, retourne NULL si aucun cas n'est sélectionné. +} + +void identify(Base* p) +{ + if (dynamic_cast(p)) std::cout << "A\n"; + else if (dynamic_cast(p)) std::cout << "B\n"; + else if (dynamic_cast(p)) std::cout << "C\n"; +} + +void identify(Base& p) +{ + try { + (void)dynamic_cast(p); std::cout << "A\n"; return; + } catch (...) {} + try { + (void)dynamic_cast(p); std::cout << "B\n"; return; + } catch (...) {} + try { + (void)dynamic_cast(p); std::cout << "C\n"; return; + } catch (...) {} +} diff --git a/cpp06/ex02/Utils.hpp b/cpp06/ex02/Utils.hpp new file mode 100644 index 0000000..89790f3 --- /dev/null +++ b/cpp06/ex02/Utils.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Utils.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:31:48 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 15:05:19 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_HPP +#define UTILS_HPP + +#include "Base.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" +#include +#include +#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" + +Base* generate(void); +void identify(Base* p); +void identify(Base& p); + +#endif \ No newline at end of file diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp new file mode 100644 index 0000000..7da448e --- /dev/null +++ b/cpp06/ex02/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/27 14:44:31 by fgras-ca #+# #+# */ +/* Updated: 2024/02/27 15:14:37 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Utils.hpp" +#include + +int main() +{ + Base* base = generate(); + + std::cout << RED << "Identify by pointer: " << RESET; + identify(base); // Identifier par pointeur + + std::cout << RED << "Identify by reference: " << RESET; + identify(*base); // Identifier par référence + + delete base; // Nettoyer la mémoire + + A a; + B b; + C c; + + std::cout << YELLOW << "\nDirect identification:" << RESET << std::endl; + + std::cout << MAGENTA << "Identify A by reference: " << RESET; + identify(a); + + std::cout << MAGENTA << "Identify B by reference: " << RESET; + identify(b); + + std::cout << MAGENTA << "Identify C by reference: " << RESET; + identify(c); + return (0); +}