diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f533d6f --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} diff --git a/ex00/Bureaucrat b/ex00/Bureaucrat new file mode 100755 index 0000000..807c307 Binary files /dev/null and b/ex00/Bureaucrat differ diff --git a/ex00/Bureaucrat.cpp b/ex00/Bureaucrat.cpp index e69de29..3e9ffae 100644 --- a/ex00/Bureaucrat.cpp +++ b/ex00/Bureaucrat.cpp @@ -0,0 +1,72 @@ +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat() : _name("default"), _grade(150) +{ +} + +Bureaucrat::Bureaucrat(const Bureaucrat ©) : _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!"); +} diff --git a/ex00/Bureaucrat.hpp b/ex00/Bureaucrat.hpp index ef8fb03..f87b332 100644 --- a/ex00/Bureaucrat.hpp +++ b/ex00/Bureaucrat.hpp @@ -18,9 +18,23 @@ class Bureaucrat ~Bureaucrat(); Bureaucrat &operator=(const Bureaucrat &other); - std::string get_name() const; - int get_grade() const; - + std::string getName() 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 diff --git a/ex00/Main.cpp b/ex00/Main.cpp index e69de29..8f68194 100644 --- a/ex00/Main.cpp +++ b/ex00/Main.cpp @@ -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); +} diff --git a/ex00/obj/Bureaucrat.o b/ex00/obj/Bureaucrat.o new file mode 100644 index 0000000..7e03711 Binary files /dev/null and b/ex00/obj/Bureaucrat.o differ diff --git a/ex00/obj/Main.o b/ex00/obj/Main.o new file mode 100644 index 0000000..b05fdaf Binary files /dev/null and b/ex00/obj/Main.o differ