d05 ex00 bureaucrat ok
This commit is contained in:
87
d05/ex00/srcs/Bureaucrat.cpp
Normal file
87
d05/ex00/srcs/Bureaucrat.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
#define COPLIEN_COLOR B_CYAN
|
||||
|
||||
/*********************************************
|
||||
* CONSTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Bureaucrat::Bureaucrat( std::string name, int grade )
|
||||
: _name(name)
|
||||
, _grade(grade) {
|
||||
if (grade > 150)
|
||||
throw GradeTooLowException();
|
||||
if (grade < 1)
|
||||
throw GradeTooHighException();
|
||||
std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat( Bureaucrat const & src ) {
|
||||
std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n";
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* DESTRUCTORS
|
||||
*********************************************/
|
||||
|
||||
Bureaucrat::~Bureaucrat() {
|
||||
std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* OPERATORS
|
||||
*********************************************/
|
||||
|
||||
Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) {
|
||||
if ( this != &rhs )
|
||||
_grade = rhs.getGrade();
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs)
|
||||
{
|
||||
o
|
||||
<< rhs.getName() << ", bureaucrat grade "
|
||||
<< rhs.getGrade();
|
||||
return (o);
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* ACCESSORS
|
||||
*********************************************/
|
||||
|
||||
std::string Bureaucrat::getName() const {return _name;}
|
||||
int Bureaucrat::getGrade() const {return _grade;}
|
||||
|
||||
/*********************************************
|
||||
* PUBLIC MEMBER FUNCTIONS
|
||||
*********************************************/
|
||||
|
||||
void Bureaucrat::gradeUp() {
|
||||
if (_grade > 1)
|
||||
_grade--;
|
||||
else
|
||||
throw GradeTooHighException();
|
||||
}
|
||||
|
||||
void Bureaucrat::gradeDown() {
|
||||
if (_grade < 150)
|
||||
_grade++;
|
||||
else
|
||||
throw GradeTooLowException();
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* NESTED CLASS
|
||||
*********************************************/
|
||||
|
||||
const char * Bureaucrat::GradeTooHighException::what() const throw() {
|
||||
return ("grade higher than 1");
|
||||
}
|
||||
const char * Bureaucrat::GradeTooLowException::what() const throw() {
|
||||
return ("grade lower than 150");
|
||||
}
|
||||
Reference in New Issue
Block a user