-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.cpp
More file actions
78 lines (73 loc) · 1.08 KB
/
Base.cpp
File metadata and controls
78 lines (73 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Base.hpp"
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include "defines.hpp"
Base * generate(void)
{
static int is_first = true;
int random_number;
Base *base = NULL;
if (is_first)
{
is_first = false;
std::srand(std::time(0));
}
random_number = std::rand() % 3;
switch (random_number)
{
case 0:
base = new A();
break;
case 1:
base = new B();
break;
case 2:
base = new C();
break;
}
return base;
}
void identify(Base* p)
{
if (dynamic_cast<A*>(p))
print (GREEN "A" RESET);
else if (dynamic_cast<B*>(p))
print (GREEN "B" RESET);
else if (dynamic_cast<C*>(p))
print (GREEN "C" RESET);
}
void identify(Base& p)
{
try
{
A &a = dynamic_cast<A&>(p);
print (GREEN "A" RESET);
(void)a;
}
catch (std::exception &e)
{
try
{
B &b = dynamic_cast<B&>(p);
print (GREEN "B" RESET);
(void)b;
}
catch (std::exception &e)
{
try
{
C &c = dynamic_cast<C&>(p);
print (GREEN "C" RESET);
(void)c;
}
catch (std::exception &e)
{
print (RED "Error: bad casing" RESET);
}
}
}
}
Base::~Base()
{
}