From bb9b81c53f00663eb1978055ce42c577abbaf30a Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Tue, 20 Feb 2024 15:37:45 +0100 Subject: [PATCH] modif --- cpp05/.vscode/settings.json | 49 +++++++++++++- cpp05/ex00/Bureaucrat.cpp | 12 ++-- cpp05/ex00/Bureaucrat.hpp | 41 ++++++------ cpp05/ex00/Bureaucrat.o | Bin 16536 -> 0 bytes cpp05/ex00/main.cpp | 2 +- cpp05/ex01/Bureaucrat.cpp | 25 +++++-- cpp05/ex01/Bureaucrat.hpp | 43 ++++++------ cpp05/ex01/Form.cpp | 25 +++++-- cpp05/ex01/Form.hpp | 15 ++--- cpp05/ex02/AForm.cpp | 104 +++++++++++++++++++++++++++++ cpp05/ex02/{Form.hpp => AForm.hpp} | 37 +++++----- cpp05/ex02/Bureaucrat.cpp | 63 +++++++++++++---- cpp05/ex02/Bureaucrat.hpp | 56 +++++++--------- cpp05/ex02/Form.cpp | 68 ------------------- cpp05/ex02/Makefile | 4 +- 15 files changed, 341 insertions(+), 203 deletions(-) delete mode 100644 cpp05/ex00/Bureaucrat.o create mode 100644 cpp05/ex02/AForm.cpp rename cpp05/ex02/{Form.hpp => AForm.hpp} (64%) delete mode 100644 cpp05/ex02/Form.cpp diff --git a/cpp05/.vscode/settings.json b/cpp05/.vscode/settings.json index 5b21dcf..d150fb8 100644 --- a/cpp05/.vscode/settings.json +++ b/cpp05/.vscode/settings.json @@ -57,7 +57,50 @@ "C_Cpp_Runner.useLinkTimeOptimization": false, "C_Cpp_Runner.msvcSecureNoWarnings": false, "files.associations": { - "iostream": "cpp", - "ostream": "cpp" -} + "iostream": "cpp", + "ostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + }, + "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/cpp05/ex00/Bureaucrat.cpp b/cpp05/ex00/Bureaucrat.cpp index b8f3482..585766a 100644 --- a/cpp05/ex00/Bureaucrat.cpp +++ b/cpp05/ex00/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ -/* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 16:48:16 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ // Constructeur par défaut Bureaucrat::Bureaucrat() : name("Default"), grade(150) { - std::cout << GREEN << "Bureaucrat default constructor called!\n" << RESET << std::endl; + std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl; } // Constructeur avec paramètres Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade) @@ -73,7 +73,8 @@ void Bureaucrat::incrementGrade() // Décrémente le grade void Bureaucrat::decrementGrade() { - if (grade >= 150) { + if (grade >= 150) + { throw GradeTooLowException(); } ++grade; @@ -81,12 +82,12 @@ void Bureaucrat::decrementGrade() // Méthodes pour les exceptions const char* Bureaucrat::GradeTooHighException::what() const throw() { - return "Grade too hight\n"; + return ("Grade too hight\n"); } const char* Bureaucrat::GradeTooLowException::what() const throw() { - return "Grade too low\n"; + return ("Grade too low\n"); } // Surcharge de l'opérateur << std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) @@ -94,4 +95,3 @@ std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade(); return (os); } - diff --git a/cpp05/ex00/Bureaucrat.hpp b/cpp05/ex00/Bureaucrat.hpp index 8547078..cc93394 100644 --- a/cpp05/ex00/Bureaucrat.hpp +++ b/cpp05/ex00/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ -/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 16:39:04 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,35 +30,34 @@ class Bureaucrat { public: - class GradeTooHighException : public std::exception + class GradeTooHighException : public std::exception { - public: - const char* what() const throw() override; - }; + public: + const char* what() const throw() override; + }; - class GradeTooLowException : public std::exception + class GradeTooLowException : public std::exception { - public: - const char* what() const throw() override; - }; + public: + const char* what() const throw() override; + }; - Bureaucrat(); - Bureaucrat(const std::string& name, int grade); - Bureaucrat(const Bureaucrat& other); - Bureaucrat& operator=(const Bureaucrat& other); - ~Bureaucrat(); + Bureaucrat(); + Bureaucrat(const std::string& name, int grade); + Bureaucrat(const Bureaucrat& other); + Bureaucrat& operator=(const Bureaucrat& other); + ~Bureaucrat(); - std::string getName() const; - int getGrade() const; - void incrementGrade(); - void decrementGrade(); + std::string getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); private: - const std::string name; - int grade; + const std::string name; + int grade; }; std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat); #endif - diff --git a/cpp05/ex00/Bureaucrat.o b/cpp05/ex00/Bureaucrat.o deleted file mode 100644 index b55510ef928995468162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7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 13:28:49 by fgras-ca #+# #+# */ -/* Updated: 2024/02/14 14:37:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 16:48:36 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp index b8f3482..b1045fa 100644 --- a/cpp05/ex01/Bureaucrat.cpp +++ b/cpp05/ex01/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ -/* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 17:30:32 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ // Constructeur par défaut Bureaucrat::Bureaucrat() : name("Default"), grade(150) { - std::cout << GREEN << "Bureaucrat default constructor called!\n" << RESET << std::endl; + std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl; } // Constructeur avec paramètres Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade) @@ -73,20 +73,34 @@ void Bureaucrat::incrementGrade() // Décrémente le grade void Bureaucrat::decrementGrade() { - if (grade >= 150) { + if (grade >= 150) + { throw GradeTooLowException(); } ++grade; } + +void Bureaucrat::signForm(Form& form) +{ + try + { + form.beSigned(*this); + std::cout << this->name << " signed " << form.getName() << std::endl; + } + catch (std::exception& e) + { + std::cout << this->name << " couldn’t sign " << form.getName() << " because " << e.what() << std::endl; + } +} // Méthodes pour les exceptions const char* Bureaucrat::GradeTooHighException::what() const throw() { - return "Grade too hight\n"; + return ("Grade too hight\n"); } const char* Bureaucrat::GradeTooLowException::what() const throw() { - return "Grade too low\n"; + return ("Grade too low\n"); } // Surcharge de l'opérateur << std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) @@ -94,4 +108,3 @@ std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade(); return (os); } - diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp index 8547078..99f03ed 100644 --- a/cpp05/ex01/Bureaucrat.hpp +++ b/cpp05/ex01/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ -/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 17:30:30 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include #include #include +#include "Form.hpp" #define RESET "\033[0m" #define BLACK "\033[30m" @@ -30,35 +31,35 @@ class Bureaucrat { public: - class GradeTooHighException : public std::exception + class GradeTooHighException : public std::exception { - public: - const char* what() const throw() override; - }; + public: + const char* what() const throw() override; + }; - class GradeTooLowException : public std::exception + class GradeTooLowException : public std::exception { - public: - const char* what() const throw() override; - }; + public: + const char* what() const throw() override; + }; - Bureaucrat(); - Bureaucrat(const std::string& name, int grade); - Bureaucrat(const Bureaucrat& other); - Bureaucrat& operator=(const Bureaucrat& other); - ~Bureaucrat(); + Bureaucrat(); + Bureaucrat(const std::string& name, int grade); + Bureaucrat(const Bureaucrat& other); + Bureaucrat& operator=(const Bureaucrat& other); + ~Bureaucrat(); - std::string getName() const; - int getGrade() const; - void incrementGrade(); - void decrementGrade(); + std::string getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); + void signForm(Form& form); private: - const std::string name; - int grade; + const std::string name; + int grade; }; std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat); #endif - diff --git a/cpp05/ex01/Form.cpp b/cpp05/ex01/Form.cpp index 505f113..93a77cd 100644 --- a/cpp05/ex01/Form.cpp +++ b/cpp05/ex01/Form.cpp @@ -6,16 +6,17 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ -/* Updated: 2024/02/11 16:05:55 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 18:19:00 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" #include "Form.hpp" -Form::Form(const std::string& name) +Form::Form() : name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1) { + std::cout << GREEN << "Form default constructor called!" << RESET << std::endl; } Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute) @@ -34,15 +35,21 @@ Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredTo Form::Form(const Form& other) : name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute) { + std::cout << CYAN << "Copy constructor called for Form " << this->name << RESET << std::endl; } + Form& Form::operator=(const Form& other) { - (void)other; + if (this != &other) + { + this->isSigned = other.isSigned; + } return (*this); } Form::~Form() { + std::cout << RED << "Form " << this->name << " is destroyed!" << RESET << std::endl; } std::string Form::getName() const @@ -77,11 +84,21 @@ void Form::beSigned(const Bureaucrat& bureaucrat) } } +const char* Form::GradeTooHighException::what() const noexcept +{ + return ("Grade too hight\n"); +} + +const char* Form::GradeTooLowException::what() const noexcept +{ + return ("Grade too low\n"); +} + std::ostream& operator<<(std::ostream& os, const Form& form) { os << "Form: " << form.getName() << ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed") << ", Grade Required to Sign: " << form.getGradeRequiredToSign() << ", Grade Required to Execute: " << form.getGradeRequiredToExecute(); - return os; + return (os); } diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp index 28798e9..d9efcd9 100644 --- a/cpp05/ex01/Form.hpp +++ b/cpp05/ex01/Form.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ -/* Updated: 2024/02/12 14:43:02 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 18:26:09 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,14 +23,13 @@ class Form { private: const std::string name; - const bool isSigned; const int gradeRequiredToSign; const int gradeRequiredToExecute; public: //constructueur - Form(const::std::string& name); + Form(); Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); // Constructeur par copie Form(const Form& other); @@ -50,19 +49,13 @@ public: class GradeTooHighException : public std::exception { public: - const char* what() const noexcept override - { - return ("Grade too high"); - } + const char* what() const noexcept override; }; class GradeTooLowException : public std::exception { public: - const char* what() const noexcept override - { - return ("Grade too low"); - } + const char* what() const noexcept override; }; }; diff --git a/cpp05/ex02/AForm.cpp b/cpp05/ex02/AForm.cpp new file mode 100644 index 0000000..af28bce --- /dev/null +++ b/cpp05/ex02/AForm.cpp @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ +/* Updated: 2024/02/16 18:44:23 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "AForm.hpp" + +AForm::AForm() + : name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1) +{ + std::cout << GREEN << "AForm default constructor called!" << RESET << std::endl; +} + +AForm::AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute) + : name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute) +{ + if (gradeRequiredToSign < 1 || gradeRequiredToExecute < 1) + { + throw GradeTooHighException(); + } + else if (gradeRequiredToSign > 150 || gradeRequiredToExecute > 150) + { + throw GradeTooLowException(); + } +} + +AForm::AForm(const AForm& other) + : name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute) +{ + std::cout << CYAN << "Copy constructor called for AForm " << this->name << RESET << std::endl; +} + +AForm& AForm::operator=(const AForm& other) +{ + if (this != &other) + { + this->isSigned = other.isSigned; + } + return (*this); +} + +AForm::~AForm() +{ + std::cout << RED << "AForm " << this->name << " is destroyed!" << RESET << std::endl; +} + +std::string AForm::getName() const +{ + return (name); +} + +bool AForm::getIsSigned() const +{ + return (isSigned); +} + +int AForm::getGradeRequiredToSign() const +{ + return (gradeRequiredToSign); +} + +int AForm::getGradeRequiredToExecute() const +{ + return (gradeRequiredToExecute); +} + +void AForm::beSigned(const Bureaucrat& bureaucrat) +{ + if (bureaucrat.getGrade() <= gradeRequiredToSign) + { + isSigned = true; + } + else + { + throw GradeTooLowException(); + } +} + +const char* AForm::GradeTooHighException::what() const noexcept +{ + return ("Grade too hight\n"); +} + +const char* AForm::GradeTooLowException::what() const noexcept +{ + return ("Grade too low\n"); +} + +std::ostream& operator<<(std::ostream& os, const AForm& aform) +{ + os << "AForm: " << aform.getName() + << ", Status: " << (aform.getIsSigned() ? "Signed" : "Not signed") + << ", Grade Required to Sign: " << aform.getGradeRequiredToSign() + << ", Grade Required to Execute: " << aform.getGradeRequiredToExecute(); + return (os); +} diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/AForm.hpp similarity index 64% rename from cpp05/ex02/Form.hpp rename to cpp05/ex02/AForm.hpp index c2c1a20..ebd02ac 100644 --- a/cpp05/ex02/Form.hpp +++ b/cpp05/ex02/AForm.hpp @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* Form.hpp :+: :+: :+: */ +/* AForm.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ -/* Updated: 2024/02/11 15:05:06 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 18:50:37 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FORM_HPP -#define FORM_HPP +#ifndef AFORM_HPP +#define AFORM_HPP #include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie #include @@ -19,7 +19,7 @@ class Bureaucrat; -class Form +class AForm { private: const std::string name; @@ -28,33 +28,38 @@ private: const int gradeRequiredToExecute; public: - Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); + //constructueur + AForm(); + AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute); + // Constructeur par copie + AForm(const AForm& other); + // Opérateur d'affectation + virtual AForm& operator=(const AForm& other); + // Destructeur + virtual ~AForm(); + + std::string getName() const; bool getIsSigned() const; int getGradeRequiredToSign() const; int getGradeRequiredToExecute() const; - void beSigned(const Bureaucrat& bureaucrat); + virtual void beSigned(const Bureaucrat& bureaucrat); class GradeTooHighException : public std::exception { public: - const char* what() const noexcept override - { - return ("Grade too high"); - } + const char* what() const noexcept override; }; class GradeTooLowException : public std::exception { public: - const char* what() const noexcept override - { - return ("Grade too low"); - } + const char* what() const noexcept override; }; }; -std::ostream& operator<<(std::ostream& os, const Form& form); +// Surcharge de l'opérateur d'insertion +std::ostream& operator<<(std::ostream& os, const AForm& aform); #endif diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp index b6f86a0..b1045fa 100644 --- a/cpp05/ex02/Bureaucrat.cpp +++ b/cpp05/ex02/Bureaucrat.cpp @@ -6,14 +6,19 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ -/* Updated: 2024/02/11 14:50:15 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 17:30:32 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" -#include "Form.hpp" -Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom) +// Constructeur par défaut +Bureaucrat::Bureaucrat() : name("Default"), grade(150) +{ + std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl; +} +// Constructeur avec paramètres +Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade) { if (grade < 1) { @@ -24,18 +29,39 @@ Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom) throw GradeTooLowException(); } this->grade = grade; + std::cout << GREEN << "Bureaucrat " << this->name << " constructor called with grade " << this->grade << RESET << std::endl; } - +// Constructeur de copie +Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade) +{ + std::cout << CYAN << "Copy constructor called for Bureaucrat " << this->name << RESET << std::endl; +} +// Opérateur d'affectation +Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) +{ + if (this != &other) + { + // L'attribut name étant const, nous ne pouvons pas le modifier. Seul grade est copié. + this->grade = other.grade; + } + return (*this); +} +// Destructeur +Bureaucrat::~Bureaucrat() +{ + std::cout << RED << "Bureaucrat " << this->name << " is destroyed!" << RESET << std::endl; +} +// Getter pour le nom std::string Bureaucrat::getName() const { - return (name); + return (this->name); } - +// Getter pour le grade int Bureaucrat::getGrade() const { - return (grade); + return (this->grade); } - +// Incrémente le grade void Bureaucrat::incrementGrade() { if (grade <= 1) @@ -44,7 +70,7 @@ void Bureaucrat::incrementGrade() } --grade; } - +// Décrémente le grade void Bureaucrat::decrementGrade() { if (grade >= 150) @@ -60,14 +86,25 @@ void Bureaucrat::signForm(Form& form) { form.beSigned(*this); std::cout << this->name << " signed " << form.getName() << std::endl; - } catch (std::exception& e) + } + catch (std::exception& e) { std::cout << this->name << " couldn’t sign " << form.getName() << " because " << e.what() << std::endl; } } - -std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat) +// Méthodes pour les exceptions +const char* Bureaucrat::GradeTooHighException::what() const throw() { - os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade(); + return ("Grade too hight\n"); +} + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade too low\n"); +} +// Surcharge de l'opérateur << +std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) +{ + os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade(); return (os); } diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp index 350c481..34fca77 100644 --- a/cpp05/ex02/Bureaucrat.hpp +++ b/cpp05/ex02/Bureaucrat.hpp @@ -6,66 +6,60 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ -/* Updated: 2024/02/11 14:59:51 by fgras-ca ### ########.fr */ +/* Updated: 2024/02/16 18:26:55 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BUREAUCRAT_HPP #define BUREAUCRAT_HPP -#include "Form.hpp" #include #include #include +#include "AForm.hpp" -#define RESET "\033[0m" -#define BLACK "\033[30m" -#define RED "\033[31m" -#define GREEN "\033[32m" -#define YELLOW "\033[33m" -#define BLUE "\033[34m" -#define MAGENTA "\033[35m" -#define CYAN "\033[36m" -#define WHITE "\033[37m" +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" class Bureaucrat { public: class GradeTooHighException : public std::exception { - public: - const char* what() const throw() override - { - return ("Grade trop élevé"); - } + public: + const char* what() const throw() override; }; class GradeTooLowException : public std::exception { - public: - const char* what() const throw() override - { - return ("Grade trop bas"); - } + public: + const char* what() const throw() override; }; -private: - const std::string name; - int grade; - -public: - Bureaucrat(const std::string &nom, int grade); // Constructeur - Bureaucrat(const Bureaucrat& other) = default; // Constructeur par copie - Bureaucrat& operator=(const Bureaucrat& other) = default; // Opérateur d'affectation - ~Bureaucrat() = default; // Destructeur + Bureaucrat(); + Bureaucrat(const std::string& name, int grade); + Bureaucrat(const Bureaucrat& other); + Bureaucrat& operator=(const Bureaucrat& other); + ~Bureaucrat(); std::string getName() const; int getGrade() const; void incrementGrade(); void decrementGrade(); void signForm(Form& form); + +private: + const std::string name; + int grade; }; -std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat); +std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat); #endif diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp deleted file mode 100644 index a632799..0000000 --- a/cpp05/ex02/Form.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* Form.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: fgras-ca +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ -/* Updated: 2024/02/11 15:03:38 by fgras-ca ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "Bureaucrat.hpp" -#include "Form.hpp" - -Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute) -: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute) -{ - if (gradeRequiredToSign < 1 || gradeRequiredToExecute < 1) - { - throw GradeTooHighException(); - } - else if (gradeRequiredToSign > 150 || gradeRequiredToExecute > 150) - { - throw GradeTooLowException(); - } -} - -std::string Form::getName() const -{ - return (name); -} - -bool Form::getIsSigned() const -{ - return (isSigned); -} - -int Form::getGradeRequiredToSign() const -{ - return (gradeRequiredToSign); -} - -int Form::getGradeRequiredToExecute() const -{ - return (gradeRequiredToExecute); -} - -void Form::beSigned(const Bureaucrat& bureaucrat) -{ - if (bureaucrat.getGrade() <= gradeRequiredToSign) - { - isSigned = true; - } - else - { - throw GradeTooLowException(); - } -} - -std::ostream& operator<<(std::ostream& os, const Form& form) -{ - os << "Form: " << form.getName() - << ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed") - << ", Grade Required to Sign: " << form.getGradeRequiredToSign() - << ", Grade Required to Execute: " << form.getGradeRequiredToExecute(); - return os; -} diff --git a/cpp05/ex02/Makefile b/cpp05/ex02/Makefile index d41a303..ca845c0 100644 --- a/cpp05/ex02/Makefile +++ b/cpp05/ex02/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # -# Updated: 2024/02/11 14:58:41 by fgras-ca ### ########.fr # +# Updated: 2024/02/16 18:36:25 by fgras-ca ### ########.fr # # # # **************************************************************************** # @@ -26,7 +26,7 @@ ORANGE = \033[38;5;214m NAME = Form SRC = Bureaucrat.cpp \ - Form.cpp \ + AForm.cpp \ main.cpp \ OBJS = ${SRC:.cpp=.o}