diff --git a/ex00/Bureaucrat b/ex00/Bureaucrat deleted file mode 100755 index 807c307..0000000 Binary files a/ex00/Bureaucrat and /dev/null differ diff --git a/ex00/obj/Bureaucrat.o b/ex00/obj/Bureaucrat.o deleted file mode 100644 index 7e03711..0000000 Binary files a/ex00/obj/Bureaucrat.o and /dev/null differ diff --git a/ex00/obj/Main.o b/ex00/obj/Main.o deleted file mode 100644 index b05fdaf..0000000 Binary files a/ex00/obj/Main.o and /dev/null differ diff --git a/ex01/Bureaucrat.cpp b/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..3e9ffae --- /dev/null +++ b/ex01/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/ex01/Bureaucrat.hpp b/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..acbacac --- /dev/null +++ b/ex01/Bureaucrat.hpp @@ -0,0 +1,39 @@ +#ifndef BUREAUCRAT_HPP +#define BUREAUCRAT_HPP + +#include +#include +#include + +class Bureaucrat +{ + private: + const std::string _name; + int _grade; + + public: + Bureaucrat(); + Bureaucrat(const std::string _name, int _grade); + Bureaucrat(const Bureaucrat ©); + Bureaucrat &operator=(const Bureaucrat &other); + + 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/ex01/Form.cpp b/ex01/Form.cpp new file mode 100644 index 0000000..cf77e88 --- /dev/null +++ b/ex01/Form.cpp @@ -0,0 +1,41 @@ +#include "Form.hpp" + +Bureaucrat::Bureaucrat() +{ +} + +Form::Form() : _name("Default"), _signed(false), _grade_si(150), _grade_ex(150) +{ +} + +Form::Form(const std::string name, const int grade_si, const int grade_ex) : _name(name), _signed(false), _grade_si(grade_si), _grade_ex(grade_ex) +{ +} + +Form::Form(const Form &other) : _name(other._name), _signed(other._signed), _grade_si(other._grade_si), _grade_ex(other._grade_ex) +{ +} + +Form::~Form() +{ +} + +std::string Form::getName() const +{ + return (_name); +} + +bool Form::getSigned() const +{ + return (_signed); +} + +int Form::getExecuteGrade() const +{ + return (_grade_ex); +} + +int Form::getSignGrade() const +{ + return (_grade_si); +} diff --git a/ex01/Form.hpp b/ex01/Form.hpp new file mode 100644 index 0000000..eef2189 --- /dev/null +++ b/ex01/Form.hpp @@ -0,0 +1,30 @@ +#ifndef FORM_HPP +#define FORM_HPP + +#include +#include +#include +#include "Bureaucrat.hpp" + +class Form +{ + private: + const std::string _name; + bool _signed; + const int _grade_ex; + const int _grade_si; + + public: + Form(); + Form(const std::string name, const int grade_si, const int grade_ex); + Form(const Form &other); + ~Form(); + Form &operator=(const Form &other); + + void beSigned(const Bureaucrat &bureaucrat); + std::string getName() const; + bool getSigned() const; + int getExecuteGrade() const; + int getSignGrade() const; +}; +#endif diff --git a/ex01/Main.cpp b/ex01/Main.cpp new file mode 100644 index 0000000..8f68194 --- /dev/null +++ b/ex01/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/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..fbdebbf --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,36 @@ +CXX = c++ +CXXFLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +SOURCES = Main.cpp Bureaucrat.cpp +OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o)) +NAME = Bureaucrat + +all: $(NAME) + +$(OBJDIR): + @echo "๐Ÿ“ Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "๐Ÿง  Compiling $< ..." + @$(CXX) $(CXXFLAGS) -c $< -o $@ + @echo "โœ… $@ ready!" + +$(NAME): $(OBJECTS) + @echo "๐Ÿ”— Linking $(NAME) ..." + @$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME) + @echo "๐ŸŽ‰ $(NAME) is ready!" + +clean: + @echo "๐Ÿงน Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "โœจ Objects cleaned!" + +fclean: clean + @echo "๐Ÿ—‘๏ธ Removing $(NAME)..." + @rm -f $(NAME) + @echo "๐Ÿ’€ Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re