diff --git a/d06/ex02/Makefile b/d06/ex02/Makefile new file mode 100644 index 0000000..dcfe758 --- /dev/null +++ b/d06/ex02/Makefile @@ -0,0 +1,75 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = dynamic + +#TYPE = c +TYPE = cpp + +ifeq "$(TYPE)" "c" + CC = c + EXT = c +else ifeq "$(TYPE)" "cpp" + CC = c++ + EXT = cpp +endif + +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +ifeq "$(TYPE)" "cpp" + CFLAGS += -std=c++98 +endif + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = . +SRCS = main.cpp + +D_HEADERS = headers +HEADERS = Classes.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) + $(CC) $(OBJS) -o $@ $(LIBS) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d06/ex02/dynamic b/d06/ex02/dynamic new file mode 100755 index 0000000..b572a32 Binary files /dev/null and b/d06/ex02/dynamic differ diff --git a/d06/ex02/headers/Classes.hpp b/d06/ex02/headers/Classes.hpp new file mode 100644 index 0000000..5624530 --- /dev/null +++ b/d06/ex02/headers/Classes.hpp @@ -0,0 +1,14 @@ +#ifndef CLASSES_HPP +# define CLASSES_HPP + +class Base { +public: + virtual ~Base() {} +}; + +class A : public Base {}; +class B : public Base {}; +class C : public Base {}; + +#endif + diff --git a/d06/ex02/main.cpp b/d06/ex02/main.cpp new file mode 100644 index 0000000..49a6100 --- /dev/null +++ b/d06/ex02/main.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +#include "Classes.hpp" + +Base * generate() { + Base *base; + + srand (time(NULL)); + int i = rand() % 3; + + if (i == 0) + { + std::cout << "A\n"; + base = new A(); + } + else if (i == 1) + { + std::cout << "B\n"; + base = new B(); + } + else + { + std::cout << "C\n"; + base = new C(); + } + + return base; +} + +void identify(Base* p) { + A * a; + B * b; + C * c; + + a = dynamic_cast(p); + if ( a != NULL) + std::cout << "A\n"; + b = dynamic_cast(p); + if ( b != NULL) + std::cout << "B\n"; + c = dynamic_cast(p); + if ( c != NULL) + std::cout << "C\n"; +} + +void identify(Base& p) { + Base base; + try { + base = dynamic_cast(p); + std::cout << " "; + } + catch ( std::exception ) {std::cout << " ";} + try { + base = dynamic_cast(p); + std::cout << " "; + } + catch ( std::exception ) {std::cout << " ";} + try { + base = dynamic_cast(p); + std::cout << " "; + } + catch ( std::exception ) {std::cout << " ";} + std::cout << "\n"; +} + +int main() { + + Base *base = generate(); + + identify(base); + identify(*base); + + delete base; + return 0; +}