diff --git a/CPP05/ex00/Bureaucrat.cpp b/CPP05/ex00/Bureaucrat.cpp index 27b637d..c30b527 100644 --- a/CPP05/ex00/Bureaucrat.cpp +++ b/CPP05/ex00/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ -/* Updated: 2022/06/24 17:42:27 by narnaud ### ########.fr */ +/* Updated: 2022/06/26 16:00:28 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,15 @@ Bureaucrat::Bureaucrat (void) { cout << "Bureaucrat default constructor called " << endl; } -Bureaucrat::Bureaucrat (string name, size_t grade) throw (char*): _name(name){ +Bureaucrat::Bureaucrat (const string name, int grade) + throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ + if (grade < 1) + throw Bureaucrat::GradeTooHighException(); if (grade > 150) - throw Bureaucrat::GradeTooHighException(""); + throw Bureaucrat::GradeTooLowException(); _grade = grade; - cout << "Bureaucrat parameter constructor called" << endl; + cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; } Bureaucrat::Bureaucrat (Bureaucrat const & src) { @@ -39,19 +42,40 @@ Bureaucrat::~Bureaucrat (void) { cout << "Bureaucrat default destructor called" << endl; } -string Bureaucrat::getName(void) { +const string Bureaucrat::getName(void) const{ return (_name); } -size_t Bureaucrat::getGrade(void) { +int Bureaucrat::getGrade(void) const{ return (_grade); } -GTHE::GTHE(void) { - +void Bureaucrat::incrGrade(int diff) + throw (Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; +} + +void Bureaucrat::decrGrade(int diff) + throw (Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; +} + +const char* Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("Grade was too high for a bureaucrat"); +} + +const char* Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("Grade was too low for a bureaucrat"); } std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { - out << b._name << ", bureaucrat grade " << b._grade << endl; + out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; + return (out); } diff --git a/CPP05/ex00/Bureaucrat.hpp b/CPP05/ex00/Bureaucrat.hpp index 3a39ed6..a0f2e05 100644 --- a/CPP05/ex00/Bureaucrat.hpp +++ b/CPP05/ex00/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ -/* Updated: 2022/06/24 16:57:34 by narnaud ### ########.fr */ +/* Updated: 2022/06/26 16:00:43 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,30 +21,31 @@ using std::string; class Bureaucrat{ string _name; - size_t _grade; + int _grade; public: - Bureaucrat(void); - Bureaucrat(string name, size_t grade) throw (char*); - Bureaucrat(Bureaucrat const & src); - virtual ~Bureaucrat(void); - Bureaucrat & operator= (Bureaucrat const & src); - string getName(void); - size_t getGrade(void); - typedef class GradeTooHighException GTHE; class GradeTooHighException: virtual public std::exception { - GTHE(void); - ~GTHE(void); - const char *GTHE::what(void); + public: + const char *what(void) const throw (); }; - typedef class GradeTooLowException GTLE; class GradeTooLowException: virtual public std::exception { - GTLE(void); - ~GTLE(void); - const char *GTLE::what(void); + public: + const char *what(void) const throw (); }; + Bureaucrat(void); + Bureaucrat(const string name, int grade) + throw (GradeTooHighException, GradeTooLowException); + Bureaucrat(Bureaucrat const & src); + virtual ~Bureaucrat(void); + Bureaucrat & operator= (Bureaucrat const & src); + const string getName(void) const; + int getGrade(void) const; + void incrGrade(int diff = 1) + throw (GradeTooHighException); + void decrGrade(int diff = 1) + throw (GradeTooLowException); }; diff --git a/CPP05/ex00/main.cpp b/CPP05/ex00/main.cpp index cc49d77..e76dd1a 100644 --- a/CPP05/ex00/main.cpp +++ b/CPP05/ex00/main.cpp @@ -6,21 +6,59 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ -/* Updated: 2022/06/24 14:38:36 by narnaud ### ########.fr */ +/* Updated: 2022/06/26 15:37:26 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" -int main(void) -{ + +void create_too_low(void) { + try { + Bureaucrat b("trash", 151); + } catch (std::exception &e) { + cout << "Grade 151 creation: " << e.what() << endl; + } +} + +void create_too_high(void) { + try { + Bureaucrat b("god", 0); + } catch (std::exception &e) { + cout << "Grade 0 creation: " << e.what() << endl; + } +} + +void first_evolve(Bureaucrat &b) { try { - Bureaucrat crat__one("first", 1); - Bureaucrat crat__two("last", 150); - Bureaucrat crat__three("trash", 151); - Bureaucrat crat__four("god", -55); + b.incrGrade(); + } catch (std::exception &e) { + cout << "Grade 1 increase: "<< e.what() << endl; } - catch (std::exception & e) { - cout << "Issue :" << e.what(); +} + +void last_dismiss(Bureaucrat &b) { + try { + b.decrGrade(); + } catch (std::exception &e) { + cout << "Grade 150 decrease: "<< e.what() << endl; } } + + +int main(void) +{ + Bureaucrat first("first", 1); + Bureaucrat last("last", 150); + + cout << first; + cout << last; + + create_too_low(); + create_too_high(); + first_evolve(first); + last_dismiss(last); + + cout << first; + cout << last; +} diff --git a/CPP05/ex01/Bureaucrat.cpp b/CPP05/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..846cdaf --- /dev/null +++ b/CPP05/ex01/Bureaucrat.cpp @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/26 15:59:50 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat (void) { + cout << "Bureaucrat default constructor called " << endl; +} + +Bureaucrat::Bureaucrat (const string name, int grade) + throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ + + if (grade < 1) + throw Bureaucrat::GradeTooHighException(); + if (grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = grade; + cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; +} + +Bureaucrat::Bureaucrat (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat copy constructor called" << endl; +} + +Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat assignment operator called" << endl; + return (*this); +} + +Bureaucrat::~Bureaucrat (void) { + cout << "Bureaucrat default destructor called << endl; +} + +const string Bureaucrat::getName(void) const{ + return (_name); +} + +int Bureaucrat::getGrade(void) const{ + return (_grade); +} + +void Bureaucrat::incrGrade(int diff) + throw (Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; +} + +void Bureaucrat::decrGrade(int diff) + throw (Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; +} + +const char* Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("Grade was too high for a bureaucrat"); +} + +const char* Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("Grade was too low for a bureaucrat"); +} + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { + out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; + return (out); +} + diff --git a/CPP05/ex01/Bureaucrat.hpp b/CPP05/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..816290c --- /dev/null +++ b/CPP05/ex01/Bureaucrat.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/26 16:35:38 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Bureaucrat{ + const string _name; + int _grade; + public: + + class GradeTooHighException: virtual public std::exception + { + public: + const char *what(void) const throw (); + }; + class GradeTooLowException: virtual public std::exception + { + public: + const char *what(void) const throw (); + }; + + Bureaucrat(void); + Bureaucrat(const string name, int grade) + throw (GradeTooHighException, GradeTooLowException); + Bureaucrat(Bureaucrat const & src); + ~Bureaucrat(void); + Bureaucrat & operator= (Bureaucrat const & src); + + const string getName(void) const; + int getGrade(void) const; + void incrGrade(int diff = 1) + throw (GradeTooHighException); + void decrGrade(int diff = 1) + throw (GradeTooLowException); + +}; + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex01/Form.cpp b/CPP05/ex01/Form.cpp new file mode 100644 index 0000000..01584b9 --- /dev/null +++ b/CPP05/ex01/Form.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ +/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + +Form::Form(const string name, const int signGrade, const int exeGrade) + throw (Form::GradeTooHighException, Form::GradeTooLowException): _name(name){ + if (signGrade < 1 || exeGrade < 1) + throw Form::GradeTooHighException; + if (signGrade > 150 || exeGrade > 150) + throw Form::GradeTooLowException; + _signGrade = signGrade; + _exeGrade = exeGrade; +} + +const string Form::getName(void) const { + return (_name); +} + +const int Form::getSignGrade(void) const { + return (_signGrade); +} + +const int Form::getExeGrade(void) const { + return (_exeGrade); +} + + diff --git a/CPP05/ex01/Form.hpp b/CPP05/ex01/Form.hpp new file mode 100644 index 0000000..d6a5ee9 --- /dev/null +++ b/CPP05/ex01/Form.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ +/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Bureaucrat.hpp" +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Form { + const string _name; + const int _signGrade; + const int _exeGrade; + bool _isSigned; + public: + class GradeTooHighException: virtual public std::exception { + public: + const char *what(void) const throw (); + } + class GradeTooLowException: virtual public std::exception { + public: + const char *what(void) const throw (); + } + Form(void); + Form(const string name, const int signGrade,const int exeGrade) + throw (GradeTooHighException, GradeTooLowException); + Form & operator=(Form const &f); + Form(Form const &f); + ~Form(); + const string getName(void) const; + const int getSignGrade(void) const; + const int getExeGrade(void) const; + beSigned(Bureaucrat); +} + +std::ostream &operator<< (std::ostream &out, const Form &f) { + out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade(); + return (out); +} diff --git a/CPP05/ex01/Makefile b/CPP05/ex01/Makefile new file mode 100644 index 0000000..35b6d46 --- /dev/null +++ b/CPP05/ex01/Makefile @@ -0,0 +1,20 @@ +NAME = bureau +SRCS = main.cpp Bureaucrat.cpp Form.cpp +OBJS= $(SRCS:.cpp=.o) + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME) : $(OBJS) + c++ $(OBJS) -o $(NAME) + +all : $(NAME) + +clean : + rm -rf $(OBJS) + +fclean : clean + rm -rf $(NAME) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/CPP05/ex01/main.cpp b/CPP05/ex01/main.cpp new file mode 100644 index 0000000..ef74330 --- /dev/null +++ b/CPP05/ex01/main.cpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ +/* Updated: 2022/06/26 16:34:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + + +void create_too_low(void) { + try { + Bureaucrat b("trash", 151); + } catch (std::exception &e) { + cout << "Grade 151 creation: " << e.what() << endl; + } +} + +void create_too_high(void) { + try { + Bureaucrat b("god", 0); + } catch (std::exception &e) { + cout << "Grade 0 creation: " << e.what() << endl; + } +} + +void first_evolve(Bureaucrat &b) { + try { + b.incrGrade(); + } catch (std::exception &e) { + cout << "Grade 1 increase: "<< e.what() << endl; + } +} + +void last_dismiss(Bureaucrat &b) { + try { + b.decrGrade(); + } catch (std::exception &e) { + cout << "Grade 150 decrease: "<< e.what() << endl; + } +} + + +int main(void) +{ + Bureaucrat first("first", 1); + Bureaucrat last("last", 150); + + cout << first; + cout << last; + + create_too_low(); + create_too_high(); + first_evolve(first); + last_dismiss(last); + + cout << first; + cout << last; +}