cpp05/ex00/Bureaucrat.cpp

73 lines
1.2 KiB
C++

#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
{
}
Bureaucrat::Bureaucrat(const Bureaucrat &copy) : _name(copy._name), _grade(copy._grade)
{
}
Bureaucrat::Bureaucrat(const std::string name, int grade) : _name(name)
{
if (grade < 1)
throw GradeTooHighException();
else if (grade > 150)
throw GradeTooLowException();
else
_grade = grade;
}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other)
{
if (this != &other)
_grade = other._grade;
return (*this);
}
Bureaucrat::~Bureaucrat()
{
}
std::string Bureaucrat::getName() const
{
return (_name);
}
int Bureaucrat::getGrade() const
{
return (_grade);
}
void Bureaucrat::incrementGrade()
{
if (_grade == 1)
throw GradeTooHighException();
else
_grade--;
}
void Bureaucrat::decrementGrade()
{
if (_grade == 150)
throw GradeTooLowException();
else
_grade++;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other)
{
os << other.getName() << ", bureaucrat grade " << other.getGrade() << std::endl;
return (os);
}
const char *Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too high!");
}
const char *Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low!");
}