d05 ex00 bureaucrat ok

This commit is contained in:
Hugo LAMY
2022-02-28 15:47:00 +01:00
parent 8f6c8259c8
commit ce1bde6d2c
7 changed files with 236 additions and 23 deletions

View File

@@ -1,14 +0,0 @@
#ifndef BUREAUCRATE_HPP
# define BUREAUCRATE_HPP
class Bureaucrate {
public:
Bureaucrate();
Bureaucrate(std::string name, int grade);
private:
std::string const _name;
int _grade;
}
#endif

View File

@@ -4,7 +4,7 @@
# . name is case sensitive . ?= set if not already set # # . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = bureaucrate NAME = bureaucrat
#CC = gcc #CC = gcc
CXX = c++ CXX = c++
@@ -21,12 +21,12 @@ LIBS =
INCLUDES = -I$(D_HEADERS) INCLUDES = -I$(D_HEADERS)
D_SRCS = . D_SRCS = srcs
SRCS = main.cpp \ SRCS = main.cpp \
Bureaucrate.cpp Bureaucrat.cpp
D_HEADERS = . D_HEADERS = headers
HEADERS = Bureaucrate.hpp HEADERS = Bureaucrat.hpp
D_OBJS = builds D_OBJS = builds
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)

BIN
d05/ex00/bureaucrat Executable file

Binary file not shown.

View File

@@ -0,0 +1,46 @@
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
# include "color.h"
# include <iostream>
# include <string>
# include <stdexcept>
class Bureaucrat {
public:
Bureaucrat(std::string name, int grade);
Bureaucrat( Bureaucrat const & src );
~Bureaucrat();
Bureaucrat & operator=( Bureaucrat const & rhs );
class GradeTooHighException : public std::exception {
public:
const char * what() const throw();
};
class GradeTooLowException : public std::exception {
public:
const char * what() const throw();
};
std::string getName() const;
int getGrade() const;
void gradeUp();
void gradeDown();
protected:
std::string const _name;
int _grade;
private:
Bureaucrat();
};
std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs);
#endif

25
d05/ex00/headers/color.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef COLOR_H
# define COLOR_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
# define RESET "\e[0m"
#endif

View File

@@ -1,10 +1,79 @@
#include "Bureaucrat.hpp"
#define N_TEST "5"
int main() { int main() {
try
{
} std::cout << B_YELLOW "\n[1/" N_TEST "] test too high :" RESET "\n";
catch (std::exception & e)
{ {
try {
Bureaucrat b("clarence", 151);
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
} }
std::cout << B_YELLOW "\n[2/" N_TEST "] test too low :" RESET "\n";
{
try {
Bureaucrat b("ellen", 0);
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
}
std::cout << B_YELLOW "\n[3/" N_TEST "] test change grade :" RESET "\n";
{
try {
Bureaucrat b("pacome", 51);
std::cout << b << "\n";
b.gradeDown();
std::cout << b << "\n";
b.gradeUp();
std::cout << b << "\n";
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
}
std::cout << B_YELLOW "\n[4/" N_TEST "] test reduce grade :" RESET "\n";
{
try
{
Bureaucrat b("colin", 145);
while (true)
{
b.gradeDown();
std::cout << b << "\n";
}
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
}
std::cout << B_YELLOW "\n[5/" N_TEST "] test increase grade :" RESET "\n";
{
try
{
Bureaucrat b("colin", 5);
while (true)
{
b.gradeUp();
std::cout << b << "\n";
}
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
}
std::cout << "\n";
return 0;
} }

View 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");
}