ex02
This commit is contained in:
parent
d6e7c6ca7e
commit
144267f134
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef A_HPP
|
||||
#define A_HPP
|
||||
|
||||
#include "Base.hpp"
|
||||
|
||||
class A: public Base
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef B_HPP
|
||||
#define B_HPP
|
||||
|
||||
#include "Base.hpp"
|
||||
|
||||
class B: public Base
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#include "Base.hpp"
|
||||
|
||||
Base::~Base()
|
||||
{
|
||||
};
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef BASE_HPP
|
||||
#define BASE_HPP
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Base
|
||||
{
|
||||
public:
|
||||
virtual ~Base();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef C_HPP
|
||||
#define C_HPP
|
||||
|
||||
#include "Base.hpp"
|
||||
|
||||
class C: public Base
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
#include "Base.hpp"
|
||||
#include "A.hpp"
|
||||
#include "B.hpp"
|
||||
#include "C.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
|
||||
// static Base *generate(void)
|
||||
// {
|
||||
// switch (rand() % 3)
|
||||
// {
|
||||
// case 0:
|
||||
// std::cout << "create A" << std::endl;
|
||||
// return (new A());
|
||||
// break;
|
||||
// case 1:
|
||||
// std::cout << "create B" << std::endl;
|
||||
// return (new B());
|
||||
// break;
|
||||
// case 2:
|
||||
// std::cout << "create C" << std::endl;
|
||||
// return (new C());
|
||||
// break;
|
||||
// default:
|
||||
// std::cerr << "Error" << std::endl;
|
||||
// return NULL;
|
||||
// }
|
||||
// }
|
||||
|
||||
static Base *generate(void)
|
||||
{
|
||||
switch (rand() % 3)
|
||||
{
|
||||
case 0:
|
||||
return (new A());
|
||||
break;
|
||||
case 1:
|
||||
return (new B());
|
||||
break;
|
||||
case 2:
|
||||
return (new C());
|
||||
break;
|
||||
default:
|
||||
std::cerr << "Error" << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void identify(Base* p)
|
||||
{
|
||||
if (dynamic_cast<A*>(p))
|
||||
std::cout << "Object is of type A" << std::endl;
|
||||
else if (dynamic_cast<B*>(p))
|
||||
std::cout << "Object is of type B" << std::endl;
|
||||
else if (dynamic_cast<C*>(p))
|
||||
std::cout << "Object is of type C" << std::endl;
|
||||
}
|
||||
|
||||
void identify(Base& p)
|
||||
{
|
||||
try {
|
||||
(void)dynamic_cast<A&>(p);
|
||||
std::cout << "Object is of type A" << std::endl;
|
||||
return;
|
||||
} catch (std::exception&) {}
|
||||
try {
|
||||
(void)dynamic_cast<B&>(p);
|
||||
std::cout << "Object is of type B" << std::endl;
|
||||
return;
|
||||
} catch (std::exception&) {}
|
||||
try {
|
||||
(void)dynamic_cast<C&>(p);
|
||||
std::cout << "Object is of type C" << std::endl;
|
||||
return;
|
||||
} catch (std::exception&) {}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Base* ptr;
|
||||
std::cout << "Test 1 PTR: " << std::endl;
|
||||
ptr = generate();
|
||||
identify(ptr);
|
||||
delete ptr;
|
||||
std::cout << "Test 2 PTR: " << std::endl;
|
||||
ptr = generate();
|
||||
identify(ptr);
|
||||
delete ptr;
|
||||
//////////////////////////////////
|
||||
std::cout << "Test 1 REF: " << std::endl;
|
||||
ptr = generate();
|
||||
identify(*ptr);
|
||||
delete ptr;
|
||||
std::cout << "Test 2 REF: " << std::endl;
|
||||
ptr = generate();
|
||||
identify(*ptr);
|
||||
delete ptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp Base.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Identify
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready!"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
Loading…
Reference in New Issue