This commit is contained in:
lfirmin 2025-11-08 22:44:40 +01:00
parent add0be53de
commit bc910a3c51
7 changed files with 184 additions and 3 deletions

47
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,47 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "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",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}

BIN
ex00/Bureaucrat Executable file

Binary file not shown.

View File

@ -0,0 +1,72 @@
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
{
}
Bureaucrat::Bureaucrat(const Bureaucrat &copy) : _name(copy._name), _grade(copy._grade)
{
}
Bureaucrat::Bureaucrat(const std::string name, int grade) : _name(name)
{
if (grade < 1)
throw GradeTooHighException();
else if (grade > 150)
throw GradeTooLowException();
else
_grade = grade;
}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other)
{
if (this != &other)
_grade = other._grade;
return (*this);
}
Bureaucrat::~Bureaucrat()
{
}
std::string Bureaucrat::getName() const
{
return (_name);
}
int Bureaucrat::getGrade() const
{
return (_grade);
}
void Bureaucrat::incrementGrade()
{
if (_grade == 1)
throw GradeTooHighException();
else
_grade--;
}
void Bureaucrat::decrementGrade()
{
if (_grade == 150)
throw GradeTooLowException();
else
_grade++;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other)
{
os << other.getName() << ", bureaucrat grade " << other.getGrade() << std::endl;
return (os);
}
const char *Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too high!");
}
const char *Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low!");
}

View File

@ -18,9 +18,23 @@ class Bureaucrat
~Bureaucrat(); ~Bureaucrat();
Bureaucrat &operator=(const Bureaucrat &other); Bureaucrat &operator=(const Bureaucrat &other);
std::string get_name() const; std::string getName() const;
int get_grade() const; int getGrade() const;
void incrementGrade();
void decrementGrade();
class GradeTooHighException : public std::exception
{
public:
virtual const char *what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
virtual const char *what() const throw();
};
}; };
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other);
#endif #endif

View File

@ -0,0 +1,48 @@
#include "Bureaucrat.hpp"
int main(void)
{
Bureaucrat cat("Moulinette", 1);
Bureaucrat stud("Student", 150);
std::cout << std::endl << "---Test too high/low create---" << std::endl;
try {
Bureaucrat test("test", 1500);
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
try {
Bureaucrat test1("test2", -10);
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
std::cout << std::endl << "---Test too high/low in/de---" << std::endl;
std::cout << cat;
try {
cat.incrementGrade();
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
std::cout << stud;
try {
stud.decrementGrade();
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
std::cout << std::endl << "---Test increment---" << std::endl;
std::cout << stud;
try {
stud.incrementGrade();
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
std::cout << stud;
std::cout << std::endl << "---Test decrement---" << std::endl;
std::cout << cat;
try {
cat.decrementGrade();
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
std::cout << cat;
return (0);
}

BIN
ex00/obj/Bureaucrat.o Normal file

Binary file not shown.

BIN
ex00/obj/Main.o Normal file

Binary file not shown.