CPP06
1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.cpp :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2025/07/22 14:54:16 by kamitsui #+# #+# */
9
/* Updated: 2025/08/15 20:40:09 by kamitsui ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
18
#include <
cstdlib
>
// For rand() and srand()
19
#include <
ctime
>
// For time()
20
#include <
iostream
>
21
22
#include "
Base.hpp
"
23
28
Base
*
generate
(
void
) {
29
int
choice =
std::rand
() % 3;
30
31
switch
(choice) {
32
case
0:
33
std::cout
<<
"Generated type: A"
<<
std::endl
;
34
return
new
A
();
35
case
1:
36
std::cout
<<
"Generated type: B"
<<
std::endl
;
37
return
new
B
();
38
case
2:
39
std::cout
<<
"Generated type: C"
<<
std::endl
;
40
return
new
C
();
41
}
42
return
NULL;
43
}
44
51
void
identify
(
Base
*p) {
52
std::cout
<<
"identify(pointer): Actual type is "
;
53
if
(
dynamic_cast<
A
*
>
(p))
54
std::cout
<<
"A"
;
55
else
if
(
dynamic_cast<
B
*
>
(p))
56
std::cout
<<
"B"
;
57
else
if
(
dynamic_cast<
C
*
>
(p))
58
std::cout
<<
"C"
;
59
else
60
std::cout
<<
"Unknown"
;
61
std::cout
<<
std::endl
;
62
}
63
73
void
identify
(
Base
&p) {
74
std::cout
<<
"identify(reference): Actual type is "
;
75
try
{
76
(void)
dynamic_cast<
A
&
>
(p);
77
std::cout
<<
"A"
;
78
}
catch
(...) {
79
try
{
80
(void)
dynamic_cast<
B
&
>
(p);
81
std::cout
<<
"B"
;
82
}
catch
(...) {
83
try
{
84
(void)
dynamic_cast<
C
&
>
(p);
85
std::cout
<<
"C"
;
86
}
catch
(...) {
87
std::cout
<<
"Unknown"
;
88
}
89
}
90
}
91
std::cout
<<
std::endl
;
92
}
93
100
int
main
() {
101
// Initialize the random seed
102
std::srand
(
std::time
(NULL));
103
104
std::cout
<<
"--- Test Run ---"
<<
std::endl
;
105
for
(
int
i = 0; i < 5; ++i) {
106
std::cout
<<
"\n[Test Case "
<< i + 1 <<
"]"
<<
std::endl
;
107
Base
*instance =
generate
();
108
109
// Identify the type using both pointer and reference
110
identify
(instance);
111
identify
(*instance);
112
113
// Prevent memory leaks
114
delete
instance;
115
}
116
117
return
0;
118
}
Base.hpp
Contains the declarations for the Base class and its derivatives.
A
An empty class that inherits from Base.
Definition
Base.hpp:40
B
An empty class that inherits from Base.
Definition
Base.hpp:46
Base
A base class with a virtual destructor.
Definition
Base.hpp:26
C
An empty class that inherits from Base.
Definition
Base.hpp:52
std::cout
cstdlib
ctime
std::endl
T endl(T... args)
main
int main()
Main function to test the Serializer.
Definition
main.cpp:28
identify
void identify(Base *p)
Identifies the actual type of an object via a pointer.
Definition
main.cpp:51
generate
Base * generate(void)
Randomly creates an instance of A, B, or C.
Definition
main.cpp:28
iostream
std::rand
T rand(T... args)
std::srand
T srand(T... args)
std::time
T time(T... args)
ex02
main.cpp
構築:
1.9.8