-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
168 lines (144 loc) · 3.75 KB
/
main.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wcorrea- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/04 19:27:13 by wcorrea- #+# #+# */
/* Updated: 2023/09/05 09:28:24 by wcorrea- ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <iomanip>
#include <cstdlib>
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
class Base
{
public:
virtual ~Base(){};
};
class A : public Base{};
class B : public Base{};
class C : public Base{};
Base * generate(void)
{
switch(std::rand() % 3)
{
case 0:
std::cout << "create new A instance" << std::endl;
return (new A());
case 1:
std::cout << "create new B instance" << std::endl;
return (new B());
case 2:
std::cout << "create new C instance" << std::endl;
return (new C());
default:
return (NULL);
}
}
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;
else
std::cout << "unknown" << std::endl;
}
void identify(Base& p)
{
try
{
(void)dynamic_cast<A&>(p);
std::cout << "A" << std::endl;
return;
}
catch(const std::exception& e) {}
try
{
(void)dynamic_cast<B&>(p);
std::cout << "B" << std::endl;
return;
}
catch(const std::exception& e) {}
try
{
(void)dynamic_cast<C&>(p);
std::cout << "C" << std::endl;
return;
}
catch(const std::exception& e)
{
std::cout << "unknown" << std::endl;
}
}
void pressEnter(void)
{
std::cout << std::endl << "press ENTER to continue" << std::endl;
std::cin.ignore();
std::cout << "\033c";
}
void titleHeader(const std::string& message)
{
std::cout << "\033c";
int standartSize = 34;
int messageSize = message.length();
int spaces = (standartSize - messageSize) / 2;
std::cout << "************************************" << std::endl << "*";
for (int i = 0; i < spaces; i++)
std::cout << " ";
std::cout << message;
for (int i = 0; i < spaces; i++)
std::cout << " ";
std::cout << "*" << std::endl << "************************************" << std::endl << std::endl;
}
int main()
{
{
titleHeader("INDENTIFY FROM POINTER");
Base *base;
for (int i = 0; i < 5; i++)
{
base = generate();
std::cout << "Base address: " << base << std::endl;
std::cout << "Base type : ";
identify(base);
std::cout << std::endl;
delete base;
}
}
pressEnter();
{
titleHeader("INDENTIFY FROM REFERENCE");
Base *base;
for (int i = 0; i < 5; i++)
{
base = generate();
std::cout << "Base address: " << base << std::endl;
std::cout << "Base type : ";
identify(*base);
std::cout << std::endl;
delete base;
}
}
pressEnter();
{
titleHeader("UNKNOWN TYPE");
Base *emptyPointer = NULL;
Base emptyClass;
std::cout << "emptyPointer address: " << emptyPointer << std::endl;
std::cout << "Base type : ";
identify(emptyPointer);
std::cout << std::endl;
std::cout << "emptyClass address : " << &emptyClass << std::endl;
std::cout << "Base type : ";
identify(emptyClass);
}
pressEnter();
}