ex02
This commit is contained in:
22
ex02/A/A.cpp
Normal file
22
ex02/A/A.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* A.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
28
ex02/A/A.hpp
Normal file
28
ex02/A/A.hpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* A.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <iostream>
|
||||||
|
# include "../Base/Base.hpp"
|
||||||
|
|
||||||
|
class A : public Base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
~A();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
22
ex02/B/B.cpp
Normal file
22
ex02/B/B.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* B.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
28
ex02/B/B.hpp
Normal file
28
ex02/B/B.hpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* B.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <iostream>
|
||||||
|
# include "../Base/Base.hpp"
|
||||||
|
|
||||||
|
class B : public Base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
~B();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
68
ex02/Base/Base.cpp
Normal file
68
ex02/Base/Base.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Base.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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<A*>(p))
|
||||||
|
std::cout << "A" << std::endl;
|
||||||
|
else if (dynamic_cast<B*>(p))
|
||||||
|
std::cout << "B" << std::endl;
|
||||||
|
else if (dynamic_cast<C*>(p))
|
||||||
|
std::cout << "C" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void identify(Base &p)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
(void)dynamic_cast<A&>(p);
|
||||||
|
std::cout << "A" << std::endl;
|
||||||
|
return ;
|
||||||
|
} catch (std::exception &e) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
(void)dynamic_cast<B&>(p);
|
||||||
|
std::cout << "B" << std::endl;
|
||||||
|
return ;
|
||||||
|
} catch (std::exception &e) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
(void)dynamic_cast<C&>(p);
|
||||||
|
std::cout << "C" << std::endl;
|
||||||
|
return ;
|
||||||
|
} catch (std::exception &e) {}
|
||||||
|
}
|
||||||
32
ex02/Base/Base.hpp
Normal file
32
ex02/Base/Base.hpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Base.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <iostream>
|
||||||
|
# include <cstdlib> // rand
|
||||||
|
|
||||||
|
class Base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~Base();
|
||||||
|
};
|
||||||
|
|
||||||
|
Base *generate();
|
||||||
|
void identify(Base *p);
|
||||||
|
void identify(Base &p);
|
||||||
|
|
||||||
|
#endif
|
||||||
22
ex02/C/C.cpp
Normal file
22
ex02/C/C.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* C.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
28
ex02/C/C.hpp
Normal file
28
ex02/C/C.hpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* C.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <iostream>
|
||||||
|
# include "../Base/Base.hpp"
|
||||||
|
|
||||||
|
class C : public Base
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
~C();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
46
ex02/Makefile
Normal file
46
ex02/Makefile
Normal file
@@ -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
|
||||||
23
ex02/main.cpp
Normal file
23
ex02/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/03/01 10:26:57 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/03/01 10:42:18 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Base/Base.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
Base *b = generate();
|
||||||
|
|
||||||
|
identify(*b);
|
||||||
|
delete(b);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user