diff --git a/ex02/A/A.cpp b/ex02/A/A.cpp new file mode 100644 index 0000000..c7e65f1 --- /dev/null +++ b/ex02/A/A.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:48 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:48 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "A.hpp" + +////////////////// +// Constructors // +////////////////// + +A::~A() +{ + // std::cout << "A destructor called" << std::endl; +} diff --git a/ex02/A/A.hpp b/ex02/A/A.hpp new file mode 100644 index 0000000..d454203 --- /dev/null +++ b/ex02/A/A.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:48 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:48 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef A_HPP + +# define A_HPP + +# include +# include "../Base/Base.hpp" + +class A : public Base +{ + private: + + public: + ~A(); +}; + +#endif diff --git a/ex02/B/B.cpp b/ex02/B/B.cpp new file mode 100644 index 0000000..38cff3f --- /dev/null +++ b/ex02/B/B.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:49 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:49 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "B.hpp" + +////////////////// +// Constructors // +////////////////// + +B::~B() +{ + // std::cout << "B destructor called" << std::endl; +} diff --git a/ex02/B/B.hpp b/ex02/B/B.hpp new file mode 100644 index 0000000..fa3b012 --- /dev/null +++ b/ex02/B/B.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:49 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:49 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_HPP + +# define B_HPP + +# include +# include "../Base/Base.hpp" + +class B : public Base +{ + private: + + public: + ~B(); +}; + +#endif diff --git a/ex02/Base/Base.cpp b/ex02/Base/Base.cpp new file mode 100644 index 0000000..ae94848 --- /dev/null +++ b/ex02/Base/Base.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:47 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:47 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" +#include "../A/A.hpp" +#include "../B/B.hpp" +#include "../C/C.hpp" + +////////////////// +// Constructors // +////////////////// + +Base::~Base() +{ + // std::cout << "Base destructor called" << std::endl; +} + +Base *generate() +{ + int r; + + r = rand() % 3; + if (r == 0) + return new A(); + if (r == 1) + return new B(); + return new C(); +} + +void identify(Base *p) +{ + if (dynamic_cast(p)) + std::cout << "A" << std::endl; + else if (dynamic_cast(p)) + std::cout << "B" << std::endl; + else if (dynamic_cast(p)) + std::cout << "C" << std::endl; +} + +void identify(Base &p) +{ + try { + (void)dynamic_cast(p); + std::cout << "A" << std::endl; + return ; + } catch (std::exception &e) {} + + try { + (void)dynamic_cast(p); + std::cout << "B" << std::endl; + return ; + } catch (std::exception &e) {} + + try { + (void)dynamic_cast(p); + std::cout << "C" << std::endl; + return ; + } catch (std::exception &e) {} +} diff --git a/ex02/Base/Base.hpp b/ex02/Base/Base.hpp new file mode 100644 index 0000000..d24f521 --- /dev/null +++ b/ex02/Base/Base.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:47 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:47 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BASE_HPP + +# define BASE_HPP + +# include +# include // rand + +class Base +{ + private: + + public: + virtual ~Base(); +}; + +Base *generate(); +void identify(Base *p); +void identify(Base &p); + +#endif diff --git a/ex02/C/C.cpp b/ex02/C/C.cpp new file mode 100644 index 0000000..aadb443 --- /dev/null +++ b/ex02/C/C.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:50 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:50 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "C.hpp" + +////////////////// +// Constructors // +////////////////// + +C::~C() +{ + // std::cout << "C destructor called" << std::endl; +} diff --git a/ex02/C/C.hpp b/ex02/C/C.hpp new file mode 100644 index 0000000..216db29 --- /dev/null +++ b/ex02/C/C.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/01 10:28:50 by aortigos #+# #+# */ +/* Updated: 2026/03/01 10:28:50 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_HPP + +# define C_HPP + +# include +# include "../Base/Base.hpp" + +class C : public Base +{ + private: + + public: + ~C(); +}; + +#endif diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..ad79ab6 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,46 @@ +NAME = type + +SRC = main.cpp \ + Base/Base.cpp \ + A/A.cpp \ + B/B.cpp \ + C/C.cpp + +OBJ = $(SRC:.cpp=.o) + +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +GREEN = \033[0;32m +RED = \033[0;31m +RESET = \033[0m + +TOTAL := $(words $(SRC)) +COUNT = 0 + +all: $(NAME) + +$(NAME): $(OBJ) + @rm -f .build_start + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) + @printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)" + +%.o: %.cpp + @if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi + @$(eval COUNT = $(shell echo $$(($(COUNT)+1)))) + @PERCENT=$$(($(COUNT)*100/$(TOTAL))); \ + BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \ + SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \ + printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL) + @$(CC) $(CFLAGS) -c $< -o $@ + +clean: + @printf "$(RED)Eliminando...\n$(RESET)" + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..12af53c --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos