CPP05 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Bureaucrat.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Bureaucrat.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/30 12:25:05 by kamitsui #+# #+# */
9/* Updated: 2025/06/30 12:25:21 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Bureaucrat.hpp"
19
20// OCF, Getters, and other methods remain largely the same
21Bureaucrat::Bureaucrat(const std::string &name, int grade) : _name(name) {
22 if (grade < 1)
24 if (grade > 150)
26 _grade = grade;
27}
28
29Bureaucrat::Bureaucrat(const Bureaucrat &other) : _name(other._name), _grade(other._grade) {}
30
32 if (this != &other) {
33 _grade = other._grade;
34 }
35 return *this;
36}
37
39
40const std::string &Bureaucrat::getName() const { return _name; }
41int Bureaucrat::getGrade() const { return _grade; }
42
44 if (_grade - 1 < 1)
46 _grade--;
47}
48
50 if (_grade + 1 > 150)
52 _grade++;
53}
54
55// Updated to work with AForm
57 try {
58 form.beSigned(*this);
59 std::cout << _name << " signed " << form.getName() << std::endl;
60 } catch (const std::exception &e) {
61 std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << "." << std::endl;
62 }
63}
64
70void Bureaucrat::executeForm(AForm const &form) {
71 try {
72 form.execute(*this);
73 std::cout << _name << " executed " << form.getName() << std::endl;
74 } catch (const std::exception &e) {
75 std::cout << _name << " couldn't execute " << form.getName() << " because " << e.what() << "." << std::endl;
76 }
77}
78
79// Exception Implementations
80const char *Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; }
81
82const char *Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; }
83
84// Operator Overload
86 os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
87 return os;
88}
An abstract base class for forms.
Definition AForm.hpp:34
void execute(Bureaucrat const &executor) const
Central execution logic. Checks requirements before calling specific action.
Definition AForm.cpp:61
const std::string & getName() const
Definition AForm.cpp:42
void beSigned(const Bureaucrat &bureaucrat)
Definition AForm.cpp:48
virtual const char * what() const
virtual const char * what() const
Represents a bureaucrat with a name and a grade.
void incrementGrade()
Increments the bureaucrat's grade (e.g., from 3 to 2).
void decrementGrade()
Decrements the bureaucrat's grade (e.g., from 2 to 3).
void signForm(Form &form)
Attempts to sign a form. Calls the form's beSigned method and prints the result.
int getGrade() const
Gets the grade of the Bureaucrat.
void executeForm(AForm const &form)
Attempts to execute a form. Calls the form's execute method and prints the result.
~Bureaucrat()
Destroy the Bureaucrat:: Bureaucrat object
Bureaucrat & operator=(const Bureaucrat &other)
Assignment operator for Bureaucrat.
const std::string & getName() const
Gets the name of the Bureaucrat.
T endl(T... args)
Defines the Bureaucrat class, updated to execute AForms.
std::ostream & operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
Overloads the << operator to print Bureaucrat information.
T what(T... args)