commit 68acc04ee8e0c5912b24c9d968b049d7cf3e47e4 Author: Camille Chauvet Date: Wed Aug 30 15:47:10 2023 +0000 init ex00 diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..47ad482 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := ex00 + +SRCS := $(wildcard $(SRCDIR)/*.cpp) +OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p obj + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR) + +fclean: clean + rm -fr $(NAME) + +re: fclean + @make --no-print-directory all diff --git a/ex00/src/Bureaucrat.cpp b/ex00/src/Bureaucrat.cpp new file mode 100644 index 0000000..cda46d4 --- /dev/null +++ b/ex00/src/Bureaucrat.cpp @@ -0,0 +1,89 @@ +#include "Bureaucrat.hpp" +#include +#include +#include + +Bureaucrat::Bureaucrat() +{ + std::cout << "Bureaucrat()" << std::endl; +} + +Bureaucrat::Bureaucrat(const Bureaucrat& src) +{ + *this = src; + std::cout << "Bureaucrat(Bureaucrat)" << std::endl; +} + +Bureaucrat::Bureaucrat(const std::string& name, int grade): + _name(name) +{ + setGrade(grade); + std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl; +} + +Bureaucrat::~Bureaucrat() +{ + std::cout << "~Bureaucrat()" << std::endl; +} + +Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src) +{ + this->_name = src._name; + this->_grade = src._grade; + + return *this; +} + +void Bureaucrat::setGrade(int new_grade) +{ + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + this->_grade = new_grade; +} + +void Bureaucrat::setName(const std::string &new_name) +{ + this->_name = new_name; +} + +const std::string& Bureaucrat::getName(void) const +{ + return this->_name; +} + +int Bureaucrat::getGrade(void) const +{ + return this->_grade; +} + +void Bureaucrat::increment() +{ + if (this->_grade == 1) + throw GradeTooHighException(); + this->_grade--; +} + +void Bureaucrat::decrement() +{ + if (this->_grade == 150) + throw GradeTooLowException(); + this->_grade++; +} + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Too small grade"; +} + +const char* Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Too big grade"; +} + +std::ostream& Bureaucrat::operator<<(std::ostream &stream) +{ + stream << this->_name << ", bureaucrat grade " << this->_grade << "." << std::endl; + return stream; +} diff --git a/ex00/src/Bureaucrat.hpp b/ex00/src/Bureaucrat.hpp new file mode 100644 index 0000000..221cf8b --- /dev/null +++ b/ex00/src/Bureaucrat.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +class Bureaucrat +{ + private: + std::string _name; + int _grade; + + public: + Bureaucrat(); + ~Bureaucrat(); + Bureaucrat(const Bureaucrat& src); + Bureaucrat(const std::string& name, int grade); + + Bureaucrat& operator=(const Bureaucrat& src); + + const std::string& getName() const; + int getGrade() const; + void setName(const std::string& new_name); + void setGrade(int new_grade); + + void increment(); + void decrement(); + + std::ostream& operator<<(std::ostream& stream); + + class GradeTooHighException: public std::exception + { + public: + const char* what() const throw(); + }; + + class GradeTooLowException: public std::exception + { + public: + const char* what() const throw(); + }; +}; +