diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-19 13:59:12 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-19 13:59:12 +0200 |
| commit | c426fe76ec5925e8c94aae3db04e5e1f1ce1585e (patch) | |
| tree | 1cdbbe15c1ed92f0df3b7041550270690575f1a2 /cpp05/ex02/Bureaucrat.cpp | |
| parent | 60c4c5309af87480fb32f3815bc02031eff43e9b (diff) | |
| download | piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.tar.gz piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.tar.bz2 piscine_cpp-c426fe76ec5925e8c94aae3db04e5e1f1ce1585e.zip | |
Adding boilerplate cpp05/ex02
Diffstat (limited to 'cpp05/ex02/Bureaucrat.cpp')
| -rw-r--r-- | cpp05/ex02/Bureaucrat.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..c9c206b --- /dev/null +++ b/cpp05/ex02/Bureaucrat.cpp @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:08:13 by charles #+# #+# */ +/* Updated: 2020/10/19 13:30:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat(Bureaucrat const& other) +{ + *this = other; +} + +Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other) +{ + m_grade = other.m_grade; + return *this; +} + +Bureaucrat::~Bureaucrat() {} + +Bureaucrat::Bureaucrat(std::string const& name, int grade) + : m_name(name), m_grade(grade) +{} + +std::string const& Bureaucrat::getName() const { return m_name; } + +int Bureaucrat::getGrade() const { return m_grade; } + +void Bureaucrat::incrementGrade() +{ + if (m_grade <= 1) + throw Bureaucrat::GradeTooHighException(); + else + m_grade--; +} + +void Bureaucrat::decrementGrade() +{ + if (m_grade >= 150) + throw Bureaucrat::GradeTooLowException(); + else + m_grade++; +} + +void Bureaucrat::signForm(Form& form) +{ + try + { + form.beSigned(); + std::cout << m_name << " signs " << form << std::endl; + } + catch (std::exception as &e) + std::cout << m_name << " cannot sign " << form + << " because " << e.what() << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +// Exceptions +/////////////////////////////////////////////////////////////////////////////// + +Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() +{} + +Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) +{} + +Bureaucrat::GradeTooHighException& +Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooHighException::~GradeTooHighException() {} + +char const* Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Grade is too high"; +} + +Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {} + +Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) + : std::exception(other) +{} + +Bureaucrat::GradeTooLowException& +Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooLowException::~GradeTooLowException() {} + +char const* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Grade is too low"; +} + +Bureaucrat::Bureaucrat() : m_name(""), m_grade(0) {} + +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b) +{ + std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl; + return out; +} |
