ex00
This commit is contained in:
89
ex00/Bureaucrat/Bureaucrat.cpp
Normal file
89
ex00/Bureaucrat/Bureaucrat.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
Bureaucrat::Bureaucrat() : name("NULL"), grade(150)
|
||||
{
|
||||
//std::cout << "Bureaucrat has been created" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const Bureaucrat &other) : name(other.getName()), grade(other.getGrade())
|
||||
{
|
||||
//std::cout << "Copy assignment called" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &other)
|
||||
{
|
||||
if (this != &other)
|
||||
this->grade = other.getGrade();
|
||||
//std::cout << "Copy operator called" << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Bureaucrat::~Bureaucrat()
|
||||
{
|
||||
//std::cout << "Bureaucrat destroyed" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(std::string name, int grade) : name(name)
|
||||
{
|
||||
if (grade > 150)
|
||||
throw GradeTooLowException();
|
||||
else if (grade < 1)
|
||||
throw GradeTooHighException();
|
||||
this->grade = grade;
|
||||
//std::cout << "Bureaucrat with params has been created" << std::endl;
|
||||
}
|
||||
|
||||
std::string Bureaucrat::getName() const
|
||||
{
|
||||
return (this->name);
|
||||
}
|
||||
|
||||
int Bureaucrat::getGrade() const
|
||||
{
|
||||
return (this->grade);
|
||||
}
|
||||
|
||||
void Bureaucrat::incrementGrade()
|
||||
{
|
||||
if (this->grade <= 1)
|
||||
throw GradeTooHighException();
|
||||
this->grade--;
|
||||
}
|
||||
|
||||
void Bureaucrat::decrementGrade()
|
||||
{
|
||||
if (this->grade >= 150)
|
||||
throw GradeTooLowException();
|
||||
this->grade++;
|
||||
}
|
||||
|
||||
const char* Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return ("Grade is too high!");
|
||||
}
|
||||
|
||||
const char* Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return ("Grade is too low!");
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||
{
|
||||
os << bureaucrat.getName()
|
||||
<< ", bureaucrat grade "
|
||||
<< bureaucrat.getGrade()
|
||||
<< std::endl;
|
||||
return (os);
|
||||
}
|
||||
52
ex00/Bureaucrat/Bureaucrat.hpp
Normal file
52
ex00/Bureaucrat/Bureaucrat.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BUREAUCRAT_HPP
|
||||
|
||||
# define BUREAUCRAT_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string name;
|
||||
int grade;
|
||||
|
||||
public:
|
||||
Bureaucrat();
|
||||
Bureaucrat(const Bureaucrat &other);
|
||||
Bureaucrat &operator=(const Bureaucrat &other);
|
||||
~Bureaucrat();
|
||||
|
||||
Bureaucrat(std::string name, int grade);
|
||||
|
||||
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, const Bureaucrat &bureaucrat);
|
||||
|
||||
#endif
|
||||
43
ex00/Makefile
Normal file
43
ex00/Makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
NAME = bureaucrat
|
||||
|
||||
SRC = main.cpp \
|
||||
Bureaucrat/Bureaucrat.cpp
|
||||
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
|
||||
GREEN = \033[0;32m
|
||||
RED = \033[0;31m
|
||||
RESET = \033[0m
|
||||
|
||||
TOTAL := $(words $(SRC))
|
||||
COUNT = 0
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@rm -f .build_start
|
||||
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
|
||||
|
||||
%.o: %.cpp
|
||||
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
|
||||
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
|
||||
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
|
||||
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
|
||||
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
|
||||
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
|
||||
@$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@printf "$(RED)Eliminando...\n$(RESET)"
|
||||
@rm -f $(OBJ)
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
104
ex00/main.cpp
Normal file
104
ex00/main.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */
|
||||
/* Updated: 2026/02/16 23:35:56 by hadi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Bureaucrat/Bureaucrat.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "---- ex00 tests ----" << std::endl;
|
||||
|
||||
try {
|
||||
std::cout << "-> Bureaucrat with grade 0" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 0);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Bureaucrat with grade 1" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 1);
|
||||
|
||||
std::cout << hadi << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Bureaucrat with grade 150" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 150);
|
||||
|
||||
std::cout << hadi << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Bureaucrat with grade 151" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 151);
|
||||
|
||||
std::cout << hadi << std::endl;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Try increment grade from 1" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 1);
|
||||
|
||||
std::cout << hadi;
|
||||
|
||||
hadi.incrementGrade();
|
||||
std::cout << hadi;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Try increment grade from 2" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 2);
|
||||
|
||||
std::cout << hadi;
|
||||
|
||||
hadi.incrementGrade();
|
||||
std::cout << hadi;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << std::endl
|
||||
<< "-> Try decrement grade from 150" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 150);
|
||||
|
||||
std::cout << hadi;
|
||||
|
||||
hadi.decrementGrade();
|
||||
std::cout << hadi;
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
std::cout << "-> Try decrement grade from 149" << std::endl;
|
||||
Bureaucrat hadi("Hadi", 149);
|
||||
|
||||
std::cout << hadi;
|
||||
hadi.decrementGrade();
|
||||
std::cout << hadi;
|
||||
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl << std::endl;
|
||||
}
|
||||
|
||||
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user