/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/26 11:58:38 by rparodi #+# #+# */ /* Updated: 2025/03/26 14:10:19 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "A.hpp" #include "B.hpp" #include "C.hpp" #include #include #include #include #include "color.hpp" /** * @brief Generate a random class (A / B / C) * * @return the pointer to the class */ 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; } }