Something went wrong. Try again.
Java--'s try/catch projects.intra.42.fr/projects/cpp-module-05
Something went wrong. Try again.
2.5 kB · 89 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#include <AForm.hpp>#include <Bureaucrat.hpp>
AForm::AForm() : name("Vente"), isSigned(false), gradeToSign(150), gradeToExecute(150) { std::cout << "AForm default constructor called" << std::endl;}
AForm::AForm(const AForm& other) : name(other.name), isSigned(other.isSigned), gradeToSign(other.gradeToSign), gradeToExecute(other.gradeToExecute) { std::cout << "AForm copy constructor called" << std::endl;}
AForm::AForm(const std::string& name) : name(name), isSigned(false), gradeToSign(150), gradeToExecute(150) { std::cout << "AForm (name) constructor called" << std::endl;}
AForm::AForm(const std::string& name, const unsigned short gradeToSign, const unsigned short gradeToExecute) : name(name), isSigned(false), gradeToSign(gradeToSign), gradeToExecute(gradeToExecute) { std::cout << "AForm (name, gradeToSign, gradeToExecute) constructor called" << std::endl;
if (gradeToSign > 150 || gradeToExecute > 150) throw GradeTooLowException();
if (gradeToSign < 1 || gradeToExecute < 1) throw GradeTooHighException();}
void AForm::beSigned(Bureaucrat& bureaucrat) { std::cout << "Be signed called" << std::endl;
if (bureaucrat.getGrade() > gradeToSign) throw GradeTooLowException();
isSigned = true;}
void AForm::execute(Bureaucrat const & executor) const { if (!isSigned) throw FormNotSignedException();
if (executor.getGrade() > gradeToExecute) throw GradeTooLowException();
executeAction();}
const std::string& AForm::getName() const { return name;}
bool AForm::getIsSigned() const { return isSigned;}
unsigned short AForm::getGradeToSign() const { return gradeToSign;}
unsigned short AForm::getGradeToExecute() const { return gradeToExecute;}
AForm& AForm::operator=(const AForm& other) { std::cout << "AForm assignment operator called" << std::endl;
if (this != &other) isSigned = other.isSigned;
return *this;}
AForm::~AForm() { std::cout << "AForm destructor called" << std::endl;}
const char* AForm::GradeTooHighException::what() const throw() { return "Grade too high";}
const char* AForm::GradeTooLowException::what() const throw() { return "Grade too low";}
const char* AForm::FormNotSignedException::what() const throw() { return "Form is not signed";}
std::ostream& operator<<(std::ostream& ostream, const AForm& form) { ostream << form.getName() << ", grade to sign " << form.getGradeToSign() << ", grade to execute " << form.getGradeToExecute() << "."; return ostream;}