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 09:58:58 by kamitsui #+# #+# */
9/* Updated: 2025/06/29 05:54:34 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Bureaucrat.hpp"
19
20// --- Orthodox Canonical Form ---
21
29Bureaucrat::Bureaucrat(const std::string &name, int grade) : _name(name) {
30 if (grade < 1) {
32 }
33 if (grade > 150) {
35 }
36 this->_grade = grade;
37 std::cout << "Bureaucrat " << this->_name << " created with grade " << this->_grade << "." << std::endl;
38}
39
44Bureaucrat::Bureaucrat(const Bureaucrat &other) : _name(other._name), _grade(other._grade) {
45 std::cout << "Bureaucrat " << this->_name << " has been copied." << std::endl;
46}
47
55 if (this != &other) {
56 this->_grade = other._grade;
57 }
58 std::cout << "Bureaucrat " << this->_name << "'s grade has been assigned from " << other._name << "." << std::endl;
59 return *this;
60}
61
65Bureaucrat::~Bureaucrat() { std::cout << "Bureaucrat " << this->_name << " has been destroyed." << std::endl; }
66
67// --- Getters ---
68
73const std::string &Bureaucrat::getName() const { return this->_name; }
74
79int Bureaucrat::getGrade() const { return this->_grade; }
80
81// --- Member Functions ---
82
88 if (this->_grade - 1 < 1) {
90 }
91 this->_grade--;
92 std::cout << "Bureaucrat " << this->_name << " grade incremented to " << this->_grade << "." << std::endl;
93}
94
100 if (this->_grade + 1 > 150) {
102 }
103 this->_grade++;
104 std::cout << "Bureaucrat " << this->_name << " grade decremented to " << this->_grade << "." << std::endl;
105}
106
107// --- Exception Implementations ---
108
113const char *Bureaucrat::GradeTooHighException::what() const throw() {
114 return "Bureaucrat Error: Grade is too high (must be >= 1)";
115}
116
121const char *Bureaucrat::GradeTooLowException::what() const throw() {
122 return "Bureaucrat Error: Grade is too low (must be <= 150)";
123}
124
125// --- Operator Overload Implementation ---
126
134 os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade() << ".";
135 return os;
136}
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).
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.
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.