diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..325429b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/settings.json +Bureaucrat +*.o + diff --git a/Makefile b/Makefile index e69de29..b1953c0 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 14:50:00 by whaffman #+# #+# # +# Updated: 2025/03/21 14:58:40 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +EX = $(shell find . -maxdepth 1 -type d -name 'ex*' -exec basename {} \;) + +all: + for ex in $(EX); do \ + $(MAKE) -C $$ex; \ + done + +clean: + for ex in $(EX); do \ + $(MAKE) -C $$ex clean; \ + done + +fclean: + for ex in $(EX); do \ + $(MAKE) -C $$ex fclean; \ + done + +re: + for ex in $(EX); do \ + $(MAKE) -C $$ex re; \ + done + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..6b13ef1 --- /dev/null +++ b/common.mk @@ -0,0 +1,36 @@ +# **************************************************************************** # +# # +# :::::::: # +# common.mk :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 15:00:16 by whaffman #+# #+# # +# Updated: 2025/03/21 15:04:44 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +INC = -I./inc +VPATH = src +# SRC = $(wildcard *.cpp) +OBJ = $(SRC:.cpp=.o) +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++11 + +all: $(NAME) + +$(NAME): $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) $(INC) -c $< -o $@ + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..1bd9e2f --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,4 @@ +NAME = Bureaucrat +SRC = main.cpp Bureaucrat.cpp +-include ../common.mk + diff --git a/ex00/inc/Bureaucrat.hpp b/ex00/inc/Bureaucrat.hpp new file mode 100644 index 0000000..5d3629d --- /dev/null +++ b/ex00/inc/Bureaucrat.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#define HIGHEST_GRADE 1 +#define LOWEST_GRADE 150 + +class Bureaucrat +{ +private: + const std::string _name; + int _grade; + +public: + Bureaucrat() = delete; + Bureaucrat(std::string name, int grade); + Bureaucrat(Bureaucrat &other); + ~Bureaucrat(); + + Bureaucrat &operator=(Bureaucrat &other) = delete; + + Bureaucrat &operator++(); // Pre-increment + Bureaucrat operator++(int); // Post-increment + Bureaucrat &operator--(); // Pre-decrement + Bureaucrat operator--(int); // Post-decrement + Bureaucrat &operator+=(int value); + Bureaucrat &operator-=(int value); + + int getGrade() const; + const std::string getName() const; + + class GradeTooHighException: public std::exception { + const char * what() const throw(); + }; + + class GradeTooLowException: public std::exception { + const char *what() const throw(); + }; +}; diff --git a/ex00/src/Bureaucrat.cpp b/ex00/src/Bureaucrat.cpp new file mode 100644 index 0000000..52c3d35 --- /dev/null +++ b/ex00/src/Bureaucrat.cpp @@ -0,0 +1,90 @@ +#include "Bureaucrat.hpp" + +#include + +Bureaucrat::Bureaucrat(std::string name, int grade): _name(name) +{ + std::cout << "Bureaucrat parameterized constructor called" << std::endl; + if (grade < HIGHEST_GRADE) + throw(Bureaucrat::GradeTooHighException()); + if (grade > LOWEST_GRADE) + throw(Bureaucrat::GradeTooLowException()); + _grade = grade; + std::cout << "Bureaucrat made with the name: " << _name << std::endl; +} + +Bureaucrat::Bureaucrat(Bureaucrat &other) :_name(other._name), _grade(other._grade) +{ + std::cout << "Bureaucrat copy constructor" << std::endl; +} + +Bureaucrat::~Bureaucrat() +{ + std::cout << "Bureaucrat deconstructor called" << std::endl; +} + +Bureaucrat &Bureaucrat::operator++() // Pre-increment +{ + if (_grade <= HIGHEST_GRADE) + throw(Bureaucrat::GradeTooHighException()); + _grade--; + return *this; +} + +Bureaucrat Bureaucrat::operator++(int) // Post-increment +{ + Bureaucrat temp(*this); + ++(*this); + return temp; +} + +Bureaucrat &Bureaucrat::operator--() // Pre-decrement +{ + if (_grade >= LOWEST_GRADE) + throw(Bureaucrat::GradeTooLowException()); + _grade++; + return *this; +} + +Bureaucrat Bureaucrat::operator--(int) // Post-decrement +{ + Bureaucrat temp(*this); + --(*this); + return temp; +} + +Bureaucrat &Bureaucrat::operator+=(int value) +{ + if (_grade - value < HIGHEST_GRADE) + throw(Bureaucrat::GradeTooHighException()); + _grade -= value; + return *this; +} + +Bureaucrat &Bureaucrat::operator-=(int value) +{ + if (_grade + value > LOWEST_GRADE) + throw(Bureaucrat::GradeTooLowException()); + _grade += value; + return *this; +} + +int Bureaucrat::getGrade() const +{ + return _grade; +} + +const std::string Bureaucrat::getName() const +{ + return _name; +} + +const char * Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Grade too high!"; +} + +const char * Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Grade too low!"; +} \ No newline at end of file diff --git a/ex00/src/main.cpp b/ex00/src/main.cpp new file mode 100644 index 0000000..f81c232 --- /dev/null +++ b/ex00/src/main.cpp @@ -0,0 +1,37 @@ +#include +#include +#include "Bureaucrat.hpp" + +int main (void) +{ + Bureaucrat jan("jan", 3); + Bureaucrat copy(jan); + + try + { + Bureaucrat bob("bob", 0); // This should throw an exception + } + catch (std::exception &e) + { + std::cerr << "Caught exception: " << e.what() << std::endl; + } + try + { + Bureaucrat alice("alice", 151); // This should also throw an exception + } + catch (std::exception &e) + { + std::cerr << "Caught exception: " << e.what() << std::endl; + } + try + { + Bureaucrat charlie("charlie", 1); + ++charlie; // This should throw an exception + } + catch (std::exception &e) + { + std::cerr << "Caught exception: " << e.what() << std::endl; + } + + +}