Something went wrong. Try again.
Java--'s try/catch projects.intra.42.fr/projects/cpp-module-05
Something went wrong. Try again.
2.7 kB · 98 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899#include <Bureaucrat.hpp>#include <AForm.hpp>
Bureaucrat::Bureaucrat() : name("Franck Leboeuf") { std::cout << "Bureaucrat default constructor called" << std::endl; setGrade(150);}
Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade) { std::cout << "Bureaucrat copy constructor called" << std::endl;}
Bureaucrat::Bureaucrat(const std::string& name) : name(name) { std::cout << "Bureaucrat (name) constructor called" << std::endl; setGrade(150);}
Bureaucrat::Bureaucrat(const std::string& name, const unsigned short grade) : name(name) { std::cout << "Bureaucrat (name, grade) constructor called" << std::endl; setGrade(grade);}
void Bureaucrat::incrementGrade() { std::cout << "Increment grade called" << std::endl; setGrade(grade - 1);}
void Bureaucrat::decrementGrade() { std::cout << "Decrement grade called" << std::endl; setGrade(grade + 1);}
void Bureaucrat::signForm(AForm& form) { if (form.getIsSigned()) { std::cout << name << " couldn't sign " << form.getName() << " because it is already signed" << std::endl; return; }
try { form.beSigned(*this); std::cout << name << " signed " << form.getName() << std::endl; } catch (const std::exception& exception) { std::cout << name << " couldn't sign " << form.getName() << " because " << exception.what() << std::endl; }}
void Bureaucrat::executeForm(AForm const & form) const { try { form.execute(*this); std::cout << name << " executed " << form.getName() << std::endl; } catch (const std::exception& exception) { std::cout << name << " couldn't execute " << form.getName() << " because " << exception.what() << std::endl; }}
const std::string& Bureaucrat::getName() const { return name;}
unsigned short Bureaucrat::getGrade() const { return grade;}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) { std::cout << "Bureaucrat assignment operator called" << std::endl;
if (this != &other) grade = other.grade;
return *this;}
Bureaucrat::~Bureaucrat() { std::cout << "Bureaucrat destructor called" << std::endl;}
void Bureaucrat::setGrade(const unsigned short grade) { if (grade > 150) throw GradeTooLowException();
if (grade < 1) throw GradeTooHighException();
this->grade = grade;}
const char* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade too high";}
const char* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade too low";}
std::ostream& operator<<(std::ostream& ostream, const Bureaucrat& bureaucrat) { ostream << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade() << "."; return ostream;}