This commit is contained in:
Ladebeze66 2024-02-14 14:43:54 +01:00
parent 04480a56eb
commit 8ad95969ba
8 changed files with 175 additions and 125 deletions

View File

@ -57,6 +57,7 @@
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"files.associations": {
"iostream": "cpp"
}
"iostream": "cpp",
"ostream": "cpp"
}
}

View File

@ -6,13 +6,19 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 13:58:43 by fgras-ca ### ########.fr */
/* Updated: 2024/02/14 14:38:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.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!\n" << RESET << std::endl;
}
// Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
{
if (grade < 1)
{
@ -23,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)
@ -43,18 +70,28 @@ void Bureaucrat::incrementGrade()
}
--grade;
}
// Décrémente le grade
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
if (grade >= 150) {
throw GradeTooLowException();
}
++grade;
}
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);
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:01:58 by fgras-ca ### ########.fr */
/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,53 +17,48 @@
#include <exception>
#include <iostream>
#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
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
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop bas");
}
};
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();
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
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
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
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

BIN
cpp05/ex00/Bureaucrat.o Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:28:49 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:08:26 by fgras-ca ### ########.fr */
/* Updated: 2024/02/14 14:37:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,14 +6,19 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:50:15 by fgras-ca ### ########.fr */
/* Updated: 2024/02/14 14:38:10 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!\n" << 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,30 +70,28 @@ void Bureaucrat::incrementGrade()
}
--grade;
}
// Décrémente le grade
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
if (grade >= 150) {
throw GradeTooLowException();
}
++grade;
}
void Bureaucrat::signForm(Form& form)
// Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl;
} catch (std::exception& e)
{
std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl;
}
return "Grade too hight\n";
}
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade();
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);
}

View File

@ -6,66 +6,59 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:59:51 by fgras-ca ### ########.fr */
/* Updated: 2024/02/14 14:32:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
#define BUREAUCRAT_HPP
#include "Form.hpp"
#include <string>
#include <exception>
#include <iostream>
#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
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
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop bas");
}
};
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();
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
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
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(Form& form);
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

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 18:03:35 by fgras-ca ### ########.fr */
/* Updated: 2024/02/12 14:43:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@
class Bureaucrat;
class AForm
class Form
{
private:
const std::string name;
@ -30,14 +30,14 @@ private:
public:
//constructueur
AForm(const::std::string& name);
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
Form(const::std::string& name);
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
AForm(const AForm& other);
Form(const Form& other);
// Opérateur d'affectation
AForm& operator=(const AForm& other);
Form& operator=(const Form& other);
// Destructeur
~AForm();
~Form();
std::string getName() const;
@ -67,6 +67,6 @@ public:
};
// Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const AForm& form);
std::ostream& operator<<(std::ostream& os, const Form& form);
#endif