ex02: init

This commit is contained in:
Camille Chauvet 2023-09-19 15:10:59 +00:00
parent 671d71f273
commit 55329e73c2
9 changed files with 119 additions and 0 deletions

26
ex02/Makefile Normal file
View File

@ -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

5
ex02/src/A.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "Base.hpp"
class A: public Base
{};

5
ex02/src/B.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "Base.hpp"
class B: public Base
{};

4
ex02/src/Base.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "Base.hpp"
Base::~Base()
{}

7
ex02/src/Base.hpp Normal file
View File

@ -0,0 +1,7 @@
#pragma once
class Base
{
public:
virtual ~Base();
};

5
ex02/src/C.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "Base.hpp"
class C: public Base
{};

23
ex02/src/generate.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <stdlib.h>
#include <time.h>
#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;
}

38
ex02/src/identify.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "identify.hpp"
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include <iostream>
void indentify(Base* p)
{
if (dynamic_cast<A*>(p) == nullptr)
std::cout << "A" << std::endl;
else if (dynamic_cast<B*>(p) == nullptr)
std::cout << "B" << std::endl;
else if (dynamic_cast<C*>(p) == nullptr)
std::cout << "C" << std::endl;
}
void identify(const Base &r)
{
try
{
(void)dynamic_cast<A>(r);
std::cout << "A" << std::endl;
return;
}
try
{
(void)dynamic_cast<B>(r)
std::cout << "B" << std::endl;
return;
}
try
{
(void)dynamic_cast<C>(r)
std::cout << "C" << std::endl;
return;
}
}

6
ex02/src/identify.hpp Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "Base.hpp"
void identify(Base* p);
void identify(Base& p);