This commit is contained in:
parent
bc910a3c51
commit
df15fdffed
BIN
ex00/Bureaucrat
BIN
ex00/Bureaucrat
Binary file not shown.
Binary file not shown.
BIN
ex00/obj/Main.o
BIN
ex00/obj/Main.o
Binary file not shown.
|
|
@ -0,0 +1,72 @@
|
|||
#include "Bureaucrat.hpp"
|
||||
|
||||
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
|
||||
{
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const Bureaucrat ©) : _name(copy._name), _grade(copy._grade)
|
||||
{
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const std::string name, int grade) : _name(name)
|
||||
{
|
||||
if (grade < 1)
|
||||
throw GradeTooHighException();
|
||||
else if (grade > 150)
|
||||
throw GradeTooLowException();
|
||||
else
|
||||
_grade = grade;
|
||||
}
|
||||
|
||||
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other)
|
||||
{
|
||||
if (this != &other)
|
||||
_grade = other._grade;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Bureaucrat::~Bureaucrat()
|
||||
{
|
||||
}
|
||||
|
||||
std::string Bureaucrat::getName() const
|
||||
{
|
||||
return (_name);
|
||||
}
|
||||
|
||||
int Bureaucrat::getGrade() const
|
||||
{
|
||||
return (_grade);
|
||||
}
|
||||
|
||||
void Bureaucrat::incrementGrade()
|
||||
{
|
||||
if (_grade == 1)
|
||||
throw GradeTooHighException();
|
||||
else
|
||||
_grade--;
|
||||
}
|
||||
|
||||
void Bureaucrat::decrementGrade()
|
||||
{
|
||||
if (_grade == 150)
|
||||
throw GradeTooLowException();
|
||||
else
|
||||
_grade++;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other)
|
||||
{
|
||||
os << other.getName() << ", bureaucrat grade " << other.getGrade() << std::endl;
|
||||
return (os);
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return ("Grade too high!");
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return ("Grade too low!");
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
#ifndef BUREAUCRAT_HPP
|
||||
#define BUREAUCRAT_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
int _grade;
|
||||
|
||||
public:
|
||||
Bureaucrat();
|
||||
Bureaucrat(const std::string _name, int _grade);
|
||||
Bureaucrat(const Bureaucrat ©);
|
||||
Bureaucrat &operator=(const Bureaucrat &other);
|
||||
|
||||
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, Bureaucrat const &other);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#include "Form.hpp"
|
||||
|
||||
Bureaucrat::Bureaucrat()
|
||||
{
|
||||
}
|
||||
|
||||
Form::Form() : _name("Default"), _signed(false), _grade_si(150), _grade_ex(150)
|
||||
{
|
||||
}
|
||||
|
||||
Form::Form(const std::string name, const int grade_si, const int grade_ex) : _name(name), _signed(false), _grade_si(grade_si), _grade_ex(grade_ex)
|
||||
{
|
||||
}
|
||||
|
||||
Form::Form(const Form &other) : _name(other._name), _signed(other._signed), _grade_si(other._grade_si), _grade_ex(other._grade_ex)
|
||||
{
|
||||
}
|
||||
|
||||
Form::~Form()
|
||||
{
|
||||
}
|
||||
|
||||
std::string Form::getName() const
|
||||
{
|
||||
return (_name);
|
||||
}
|
||||
|
||||
bool Form::getSigned() const
|
||||
{
|
||||
return (_signed);
|
||||
}
|
||||
|
||||
int Form::getExecuteGrade() const
|
||||
{
|
||||
return (_grade_ex);
|
||||
}
|
||||
|
||||
int Form::getSignGrade() const
|
||||
{
|
||||
return (_grade_si);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef FORM_HPP
|
||||
#define FORM_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
class Form
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
bool _signed;
|
||||
const int _grade_ex;
|
||||
const int _grade_si;
|
||||
|
||||
public:
|
||||
Form();
|
||||
Form(const std::string name, const int grade_si, const int grade_ex);
|
||||
Form(const Form &other);
|
||||
~Form();
|
||||
Form &operator=(const Form &other);
|
||||
|
||||
void beSigned(const Bureaucrat &bureaucrat);
|
||||
std::string getName() const;
|
||||
bool getSigned() const;
|
||||
int getExecuteGrade() const;
|
||||
int getSignGrade() const;
|
||||
};
|
||||
#endif
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
#include "Bureaucrat.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Bureaucrat cat("Moulinette", 1);
|
||||
Bureaucrat stud("Student", 150);
|
||||
std::cout << std::endl << "---Test too high/low create---" << std::endl;
|
||||
try {
|
||||
Bureaucrat test("test", 1500);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
try {
|
||||
Bureaucrat test1("test2", -10);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl << "---Test too high/low in/de---" << std::endl;
|
||||
std::cout << cat;
|
||||
try {
|
||||
cat.incrementGrade();
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << stud;
|
||||
try {
|
||||
stud.decrementGrade();
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl << "---Test increment---" << std::endl;
|
||||
std::cout << stud;
|
||||
try {
|
||||
stud.incrementGrade();
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << stud;
|
||||
std::cout << std::endl << "---Test decrement---" << std::endl;
|
||||
std::cout << cat;
|
||||
try {
|
||||
cat.decrementGrade();
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << cat;
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp Bureaucrat.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Bureaucrat
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready!"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
Loading…
Reference in New Issue