Files
cpp06/ex02/Base/Base.cpp
2026-03-01 10:43:15 +01:00

69 lines
1.9 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Base.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/01 10:28:47 by aortigos #+# #+# */
/* Updated: 2026/03/01 10:28:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Base.hpp"
#include "../A/A.hpp"
#include "../B/B.hpp"
#include "../C/C.hpp"
//////////////////
// Constructors //
//////////////////
Base::~Base()
{
// std::cout << "Base destructor called" << std::endl;
}
Base *generate()
{
int r;
r = rand() % 3;
if (r == 0)
return new A();
if (r == 1)
return new B();
return new C();
}
void identify(Base *p)
{
if (dynamic_cast<A*>(p))
std::cout << "A" << std::endl;
else if (dynamic_cast<B*>(p))
std::cout << "B" << std::endl;
else if (dynamic_cast<C*>(p))
std::cout << "C" << std::endl;
}
void identify(Base &p)
{
try {
(void)dynamic_cast<A&>(p);
std::cout << "A" << std::endl;
return ;
} catch (std::exception &e) {}
try {
(void)dynamic_cast<B&>(p);
std::cout << "B" << std::endl;
return ;
} catch (std::exception &e) {}
try {
(void)dynamic_cast<C&>(p);
std::cout << "C" << std::endl;
return ;
} catch (std::exception &e) {}
}