d06 ex02
This commit is contained in:
75
d06/ex02/Makefile
Normal file
75
d06/ex02/Makefile
Normal file
@@ -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
|
||||||
|
|
||||||
BIN
d06/ex02/dynamic
Executable file
BIN
d06/ex02/dynamic
Executable file
Binary file not shown.
14
d06/ex02/headers/Classes.hpp
Normal file
14
d06/ex02/headers/Classes.hpp
Normal file
@@ -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
|
||||||
|
|
||||||
77
d06/ex02/main.cpp
Normal file
77
d06/ex02/main.cpp
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
#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<A *>(p);
|
||||||
|
if ( a != NULL)
|
||||||
|
std::cout << "A\n";
|
||||||
|
b = dynamic_cast<B *>(p);
|
||||||
|
if ( b != NULL)
|
||||||
|
std::cout << "B\n";
|
||||||
|
c = dynamic_cast<C *>(p);
|
||||||
|
if ( c != NULL)
|
||||||
|
std::cout << "C\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void identify(Base& p) {
|
||||||
|
Base base;
|
||||||
|
try {
|
||||||
|
base = dynamic_cast<A&>(p);
|
||||||
|
std::cout << "<A> ";
|
||||||
|
}
|
||||||
|
catch ( std::exception ) {std::cout << "<not A...> ";}
|
||||||
|
try {
|
||||||
|
base = dynamic_cast<B&>(p);
|
||||||
|
std::cout << "<B> ";
|
||||||
|
}
|
||||||
|
catch ( std::exception ) {std::cout << "<not B...> ";}
|
||||||
|
try {
|
||||||
|
base = dynamic_cast<C&>(p);
|
||||||
|
std::cout << "<C> ";
|
||||||
|
}
|
||||||
|
catch ( std::exception ) {std::cout << "<not C...> ";}
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
Base *base = generate();
|
||||||
|
|
||||||
|
identify(base);
|
||||||
|
identify(*base);
|
||||||
|
|
||||||
|
delete base;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user