Compare commits
2 Commits
9bea74be7e
...
b4b44ee39b
| Author | SHA1 | Date | |
|---|---|---|---|
| b4b44ee39b | |||
| 9389f14dcd |
132
ex01/Bureaucrat/Bureaucrat.cpp
Normal file
132
ex01/Bureaucrat/Bureaucrat.cpp
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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"
|
||||||
|
#include "../Form/Form.hpp"
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
//Constructores //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Getters //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
std::string Bureaucrat::getName() const
|
||||||
|
{
|
||||||
|
return (this->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Bureaucrat::getGrade() const
|
||||||
|
{
|
||||||
|
return (this->grade);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Grade //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
void Bureaucrat::incrementGrade()
|
||||||
|
{
|
||||||
|
if (this->grade <= 1)
|
||||||
|
throw GradeTooHighException();
|
||||||
|
this->grade--;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bureaucrat::decrementGrade()
|
||||||
|
{
|
||||||
|
if (this->grade >= 150)
|
||||||
|
throw GradeTooLowException();
|
||||||
|
this->grade++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bureaucrat::signForm(Form &form)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
form.beSigned(*this);
|
||||||
|
std::cout << this->name
|
||||||
|
<< " signed "
|
||||||
|
<< form.getName()
|
||||||
|
<< std::endl;
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
std::cout << this->name
|
||||||
|
<< "couldn't sign "
|
||||||
|
<< form.getName()
|
||||||
|
<< " because "
|
||||||
|
<< e.what()
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// << //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||||
|
{
|
||||||
|
os << bureaucrat.getName()
|
||||||
|
<< ", bureaucrat grade "
|
||||||
|
<< bureaucrat.getGrade()
|
||||||
|
<< std::endl;
|
||||||
|
return (os);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Excepciones //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
const char* Bureaucrat::GradeTooHighException::what() const throw()
|
||||||
|
{
|
||||||
|
return ("Grade is too high!");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* Bureaucrat::GradeTooLowException::what() const throw()
|
||||||
|
{
|
||||||
|
return ("Grade is too low!");
|
||||||
|
}
|
||||||
56
ex01/Bureaucrat/Bureaucrat.hpp
Normal file
56
ex01/Bureaucrat/Bureaucrat.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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 Form;
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
void signForm(Form &form);
|
||||||
|
|
||||||
|
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
|
||||||
124
ex01/Form/Form.cpp
Normal file
124
ex01/Form/Form.cpp
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Form.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 "Form.hpp"
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
//Constructores //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
Form::Form() : name("NULL"), isSigned(false), gradeToSign(1), gradeToExecute(1)
|
||||||
|
{
|
||||||
|
//std::cout << "Form default constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Form::Form(std::string name, bool isSigned, int gradeToSign, int gradeToExecute)
|
||||||
|
: name(name), isSigned(isSigned),
|
||||||
|
gradeToSign(gradeToSign), gradeToExecute(gradeToExecute)
|
||||||
|
{
|
||||||
|
if (gradeToSign > 150 || gradeToExecute > 150)
|
||||||
|
throw GradeTooLowException();
|
||||||
|
else if (gradeToSign < 1 || gradeToExecute < 1)
|
||||||
|
throw GradeTooHighException();
|
||||||
|
//std::cout << "Form constructor with params called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Form::Form(const Form &other) :
|
||||||
|
name (other.getName()), isSigned(other.getIsSigned()),
|
||||||
|
gradeToSign(other.getGradeToSign()),
|
||||||
|
gradeToExecute(other.getGradeToExecute())
|
||||||
|
{
|
||||||
|
//std::cout << "Form copy constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Form& Form::operator=(const Form &other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
this->isSigned = other.getIsSigned();
|
||||||
|
//std::cout << "Form copy assigment operator called" << std::endl;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Form::~Form()
|
||||||
|
{
|
||||||
|
//std::cout << "Destructor called"
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Getters //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
std::string Form::getName() const
|
||||||
|
{
|
||||||
|
return (this->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Form::getIsSigned() const
|
||||||
|
{
|
||||||
|
return (this->isSigned);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Form::getGradeToSign() const
|
||||||
|
{
|
||||||
|
return (this->gradeToSign);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Form::getGradeToExecute() const
|
||||||
|
{
|
||||||
|
return (this->gradeToExecute);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// SignForm //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
void Form::beSigned(const Bureaucrat &br)
|
||||||
|
{
|
||||||
|
if (this->gradeToSign < br.getGrade())
|
||||||
|
throw GradeTooLowException();
|
||||||
|
this->isSigned = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// << //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream &os, const Form &form)
|
||||||
|
{
|
||||||
|
std::string isItSigned;
|
||||||
|
|
||||||
|
if (form.getIsSigned())
|
||||||
|
{
|
||||||
|
isItSigned = " is signed.";
|
||||||
|
} else {
|
||||||
|
isItSigned = " is not signed.";
|
||||||
|
}
|
||||||
|
|
||||||
|
os << form.getName() << isItSigned;
|
||||||
|
return (os);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Excepciones //
|
||||||
|
//////////////////
|
||||||
|
const char* Form::GradeTooLowException::what() const throw()
|
||||||
|
{
|
||||||
|
return ("grade too low");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* Form::GradeTooHighException::what() const throw()
|
||||||
|
{
|
||||||
|
return ("grade too high");
|
||||||
|
}
|
||||||
56
ex01/Form/Form.hpp
Normal file
56
ex01/Form/Form.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Form.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 FORM_HPP
|
||||||
|
|
||||||
|
# define FORM_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include "../Bureaucrat/Bureaucrat.hpp"
|
||||||
|
|
||||||
|
class Form
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
const std::string name;
|
||||||
|
bool isSigned;
|
||||||
|
const int gradeToSign;
|
||||||
|
const int gradeToExecute;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Form();
|
||||||
|
Form(const Form &other);
|
||||||
|
Form& operator=(const Form &other);
|
||||||
|
~Form();
|
||||||
|
|
||||||
|
Form(std::string name, bool isSigned, int gradeToSign, int gradeToExecute);
|
||||||
|
|
||||||
|
std::string getName() const;
|
||||||
|
bool getIsSigned() const;
|
||||||
|
int getGradeToSign() const;
|
||||||
|
int getGradeToExecute() const;
|
||||||
|
|
||||||
|
void beSigned(const Bureaucrat &br);
|
||||||
|
|
||||||
|
class GradeTooLowException : public std::exception {
|
||||||
|
public:
|
||||||
|
virtual const char *what() const throw();
|
||||||
|
};
|
||||||
|
|
||||||
|
class GradeTooHighException : public std::exception {
|
||||||
|
public:
|
||||||
|
virtual const char *what() const throw();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream &os, const Form &form);
|
||||||
|
|
||||||
|
#endif
|
||||||
43
ex01/Makefile
Normal file
43
ex01/Makefile
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
NAME = form
|
||||||
|
|
||||||
|
SRC = main.cpp \
|
||||||
|
Bureaucrat/Bureaucrat.cpp Form/Form.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
|
||||||
35
ex01/main.cpp
Normal file
35
ex01/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */
|
||||||
|
/* Updated: 2026/02/17 09:48:55 by hadi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Bureaucrat/Bureaucrat.hpp"
|
||||||
|
#include "Form/Form.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "---- ex01 tests ----" << std::endl;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Form form("AngelForm", false, 139, 5);
|
||||||
|
Bureaucrat angel("Angel", 140);
|
||||||
|
|
||||||
|
std::cout << form << std::endl;
|
||||||
|
angel.signForm(form);
|
||||||
|
std::cout << form << std::endl;
|
||||||
|
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user