CPP05
1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.cpp :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2025/06/30 12:43:18 by kamitsui #+# #+# */
9
/* Updated: 2025/06/30 12:50:30 by kamitsui ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
21
#include "
AForm.hpp
"
22
#include "
Bureaucrat.hpp
"
23
#include "
Intern.hpp
"
24
#include "
PresidentialPardonForm.hpp
"
25
#include "
RobotomyRequestForm.hpp
"
26
#include "
ShrubberyCreationForm.hpp
"
27
#include <
cstdlib
>
28
#include <
ctime
>
29
#include <
iostream
>
30
31
void
print_header
(
const
std::string
&title) {
std::cout
<<
"\n\033[1;33m--- "
<< title <<
" ---\033[0m"
<<
std::endl
; }
32
33
int
main
() {
34
// Seed the random number generator
35
srand(time(NULL));
36
37
// Create an Intern and a high-level Bureaucrat to test the forms
38
Intern
someRandomIntern;
39
Bureaucrat
boss(
"Zaphod"
, 1);
40
AForm
*form;
41
42
// --- Test 1: Create a Robotomy Request Form ---
43
print_header
(
"Test 1: Create Robotomy Request"
);
44
try
{
45
form = someRandomIntern.
makeForm
(
"robotomy request"
,
"Bender"
);
46
if
(form) {
47
std::cout
<< boss <<
std::endl
;
48
std::cout
<< *form <<
std::endl
;
49
boss.
signForm
(*form);
50
boss.
executeForm
(*form);
51
delete
form;
// Clean up the allocated memory
52
}
53
}
catch
(
const
std::exception
&e) {
54
std::cerr
<< e.
what
() <<
std::endl
;
55
}
56
57
// --- Test 2: Create a Shrubbery Creation Form ---
58
print_header
(
"Test 2: Create Shrubbery Creation"
);
59
try
{
60
form = someRandomIntern.
makeForm
(
"shrubbery creation"
,
"home"
);
61
if
(form) {
62
std::cout
<< boss <<
std::endl
;
63
std::cout
<< *form <<
std::endl
;
64
boss.
signForm
(*form);
65
boss.
executeForm
(*form);
66
delete
form;
67
}
68
}
catch
(
const
std::exception
&e) {
69
std::cerr
<< e.
what
() <<
std::endl
;
70
}
71
72
// --- Test 3: Create a Presidential Pardon Form ---
73
print_header
(
"Test 3: Create Presidential Pardon"
);
74
try
{
75
form = someRandomIntern.
makeForm
(
"presidential pardon"
,
"Ford Prefect"
);
76
if
(form) {
77
std::cout
<< boss <<
std::endl
;
78
std::cout
<< *form <<
std::endl
;
79
boss.
signForm
(*form);
80
boss.
executeForm
(*form);
81
delete
form;
82
}
83
}
catch
(
const
std::exception
&e) {
84
std::cerr
<< e.
what
() <<
std::endl
;
85
}
86
87
// --- Test 4: Attempt to create an unknown form ---
88
print_header
(
"Test 4: Create Unknown Form"
);
89
try
{
90
form = someRandomIntern.
makeForm
(
"lunch request"
,
"the kitchen"
);
91
if
(form) {
// This block should not be reached
92
std::cout
<<
"This should not print."
<<
std::endl
;
93
delete
form;
94
}
95
}
catch
(
const
std::exception
&e) {
96
std::cerr
<<
"\033[1;31mCaught expected exception: "
<< e.
what
() <<
"\033[0m"
<<
std::endl
;
97
}
98
99
print_header
(
"All tests complete."
);
100
return
0;
101
}
Intern.hpp
Defines the Intern class, which can create forms.
std::string
std::cerr
AForm
An abstract base class for forms.
Definition
AForm.hpp:34
Bureaucrat
Represents a bureaucrat with a name and a grade.
Definition
Bureaucrat.hpp:30
Bureaucrat::signForm
void signForm(Form &form)
Attempts to sign a form. Calls the form's beSigned method and prints the result.
Definition
Bureaucrat.cpp:68
Bureaucrat::executeForm
void executeForm(AForm const &form)
Attempts to execute a form. Calls the form's execute method and prints the result.
Definition
Bureaucrat.cpp:70
Intern
Definition
Intern.hpp:36
Intern::makeForm
AForm * makeForm(const std::string &formName, const std::string &formTarget)
Definition
Intern.cpp:45
std::cout
cstdlib
ctime
std::endl
T endl(T... args)
print_header
void print_header(const std::string &title)
Prints a formatted header to the console.
Definition
main.cpp:28
main
int main()
Main entry point of the program.
Definition
main.cpp:34
AForm.hpp
Defines the abstract base class AForm for all specific forms.
Bureaucrat.hpp
Defines the Bureaucrat class, updated to execute AForms.
PresidentialPardonForm.hpp
Defines the PresidentialPardonForm class.
RobotomyRequestForm.hpp
Defines the RobotomyRequestForm class.
ShrubberyCreationForm.hpp
Defines the ShrubberyCreationForm class.
std::exception
iostream
std::exception::what
T what(T... args)
ex03
main.cpp
構築:
1.9.8