CPP05 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Bureaucrat.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Bureaucrat.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/26 10:33:05 by kamitsui #+# #+# */
9/* Updated: 2025/06/29 06:09:46 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Bureaucrat.hpp"
19
20// --- Orthodox Canonical Form ---
21Bureaucrat::Bureaucrat(const std::string &name, int grade) : _name(name) {
22 if (grade < 1)
24 if (grade > 150)
26 _grade = grade;
27 std::cout << "Bureaucrat " << _name << " created with grade " << _grade << "." << std::endl;
28}
29
30Bureaucrat::Bureaucrat(const Bureaucrat &other) : _name(other._name), _grade(other._grade) {
31 std::cout << "Bureaucrat " << _name << " has been copied." << std::endl;
32}
33
35 if (this != &other) {
36 _grade = other._grade;
37 }
38 std::cout << "Bureaucrat " << _name << "'s grade has been assigned from " << other._name << "." << std::endl;
39 return *this;
40}
41
42Bureaucrat::~Bureaucrat() { std::cout << "Bureaucrat " << _name << " has been destroyed." << std::endl; }
43
44// --- Getters ---
45const std::string &Bureaucrat::getName() const { return _name; }
46int Bureaucrat::getGrade() const { return _grade; }
47
48// --- Member Functions ---
50 if (_grade - 1 < 1)
52 _grade--;
53 std::cout << "Bureaucrat " << _name << " grade incremented to " << _grade << "." << std::endl;
54}
55
57 if (_grade + 1 > 150)
59 _grade++;
60 std::cout << "Bureaucrat " << _name << " grade decremented to " << _grade << "." << std::endl;
61}
62
69 try {
70 form.beSigned(*this);
71 std::cout << _name << " signed " << form.getName() << std::endl;
72 } catch (const std::exception &e) {
73 std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << "." << std::endl;
74 }
75}
76
77// --- Exception Implementations ---
78const char *Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; }
79
80const char *Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; }
81
82// --- Operator Overload ---
84 os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
85 return os;
86}
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.
~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.
Represents a form that needs to be signed and executed.
Definition Form.hpp:32
void beSigned(const Bureaucrat &bureaucrat)
Sets the form's status to signed if the bureaucrat's grade is sufficient.
Definition Form.cpp:79
const std::string & getName() const
Definition Form.cpp:68
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)