diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9b9a653 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := ex02 + +SRCS := $(wildcard $(SRCDIR)/*.cpp) +OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p $(OBJDIR) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR) + +fclean: clean + rm -fr $(NAME) + +re: fclean + @make --no-print-directory all diff --git a/ex02/src/A.hpp b/ex02/src/A.hpp new file mode 100644 index 0000000..83fdfe3 --- /dev/null +++ b/ex02/src/A.hpp @@ -0,0 +1,5 @@ +#pragma once +#include "Base.hpp" + +class A: public Base +{}; diff --git a/ex02/src/B.hpp b/ex02/src/B.hpp new file mode 100644 index 0000000..9d28f48 --- /dev/null +++ b/ex02/src/B.hpp @@ -0,0 +1,5 @@ +#pragma once +#include "Base.hpp" + +class B: public Base +{}; diff --git a/ex02/src/Base.cpp b/ex02/src/Base.cpp new file mode 100644 index 0000000..fb4d862 --- /dev/null +++ b/ex02/src/Base.cpp @@ -0,0 +1,4 @@ +#include "Base.hpp" + +Base::~Base() +{} diff --git a/ex02/src/Base.hpp b/ex02/src/Base.hpp new file mode 100644 index 0000000..23db806 --- /dev/null +++ b/ex02/src/Base.hpp @@ -0,0 +1,7 @@ +#pragma once + +class Base +{ + public: + virtual ~Base(); +}; diff --git a/ex02/src/C.hpp b/ex02/src/C.hpp new file mode 100644 index 0000000..3058003 --- /dev/null +++ b/ex02/src/C.hpp @@ -0,0 +1,5 @@ +#pragma once +#include "Base.hpp" + +class C: public Base +{}; diff --git a/ex02/src/generate.cpp b/ex02/src/generate.cpp new file mode 100644 index 0000000..cde233d --- /dev/null +++ b/ex02/src/generate.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include "Base.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +Base* genere(void) +{ + + std::srand(time(NULL)); + + switch (rand() % 3) { + case 0: + return new A; + case 1: + return new B; + case 2: + return new C; + } + return NULL; +} diff --git a/ex02/src/identify.cpp b/ex02/src/identify.cpp new file mode 100644 index 0000000..bf0a540 --- /dev/null +++ b/ex02/src/identify.cpp @@ -0,0 +1,38 @@ +#include "identify.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +#include + +void indentify(Base* p) +{ + if (dynamic_cast(p) == nullptr) + std::cout << "A" << std::endl; + else if (dynamic_cast(p) == nullptr) + std::cout << "B" << std::endl; + else if (dynamic_cast(p) == nullptr) + std::cout << "C" << std::endl; +} + +void identify(const Base &r) +{ + try + { + (void)dynamic_cast(r); + std::cout << "A" << std::endl; + return; + } + try + { + (void)dynamic_cast(r) + std::cout << "B" << std::endl; + return; + } + try + { + (void)dynamic_cast(r) + std::cout << "C" << std::endl; + return; + } +} diff --git a/ex02/src/identify.hpp b/ex02/src/identify.hpp new file mode 100644 index 0000000..cbca540 --- /dev/null +++ b/ex02/src/identify.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "Base.hpp" + +void identify(Base* p); +void identify(Base& p);