first version of ex00
This commit is contained in:
parent
1e75a92c41
commit
c9639437ed
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.vscode/settings.json
|
||||||
|
Bureaucrat
|
||||||
|
*.o
|
||||||
|
|
||||||
35
Makefile
35
Makefile
@ -0,0 +1,35 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
36
common.mk
Normal file
36
common.mk
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# common.mk :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
4
ex00/Makefile
Normal file
4
ex00/Makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
NAME = Bureaucrat
|
||||||
|
SRC = main.cpp Bureaucrat.cpp
|
||||||
|
-include ../common.mk
|
||||||
|
|
||||||
40
ex00/inc/Bureaucrat.hpp
Normal file
40
ex00/inc/Bureaucrat.hpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
#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();
|
||||||
|
};
|
||||||
|
};
|
||||||
90
ex00/src/Bureaucrat.cpp
Normal file
90
ex00/src/Bureaucrat.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include "Bureaucrat.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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!";
|
||||||
|
}
|
||||||
37
ex00/src/main.cpp
Normal file
37
ex00/src/main.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user