init ex00

This commit is contained in:
Camille Chauvet
2023-08-30 15:47:10 +00:00
commit 68acc04ee8
3 changed files with 157 additions and 0 deletions

89
ex00/src/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "Bureaucrat.hpp"
#include <iostream>
#include <ostream>
#include <string>
Bureaucrat::Bureaucrat()
{
std::cout << "Bureaucrat()" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat& src)
{
*this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl;
}
Bureaucrat::Bureaucrat(const std::string& name, int grade):
_name(name)
{
setGrade(grade);
std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl;
}
Bureaucrat::~Bureaucrat()
{
std::cout << "~Bureaucrat()" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
{
this->_name = src._name;
this->_grade = src._grade;
return *this;
}
void Bureaucrat::setGrade(int new_grade)
{
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
this->_grade = new_grade;
}
void Bureaucrat::setName(const std::string &new_name)
{
this->_name = new_name;
}
const std::string& Bureaucrat::getName(void) const
{
return this->_name;
}
int Bureaucrat::getGrade(void) const
{
return this->_grade;
}
void Bureaucrat::increment()
{
if (this->_grade == 1)
throw GradeTooHighException();
this->_grade--;
}
void Bureaucrat::decrement()
{
if (this->_grade == 150)
throw GradeTooLowException();
this->_grade++;
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "Too small grade";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "Too big grade";
}
std::ostream& Bureaucrat::operator<<(std::ostream &stream)
{
stream << this->_name << ", bureaucrat grade " << this->_grade << "." << std::endl;
return stream;
}