add: ex01 without main

This commit is contained in:
Camille Chauvet 2023-08-31 16:03:36 +00:00
parent 02d0932e9d
commit f080860868
6 changed files with 350 additions and 0 deletions

26
ex01/Makefile Normal file
View File

@ -0,0 +1,26 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex00
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)
fclean: clean
rm -fr $(NAME)
re: fclean
@make --no-print-directory all

88
ex01/src/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,88 @@
#include "Bureaucrat.hpp"
#include <iostream>
#include <ostream>
#include <string>
Bureaucrat::Bureaucrat()
{
std::cout << "Bureaucrat()" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat& src)
{
*this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl;
}
Bureaucrat::Bureaucrat(const std::string& name, int grade):
_name(name)
{
setGrade(grade);
std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl;
}
Bureaucrat::~Bureaucrat()
{
std::cout << "~Bureaucrat()" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
{
this->_grade = src._grade;
return *this;
}
void Bureaucrat::setGrade(int new_grade)
{
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
this->_grade = new_grade;
}
const std::string& Bureaucrat::getName(void) const
{
return this->_name;
}
int Bureaucrat::getGrade(void) const
{
return this->_grade;
}
void Bureaucrat::increment()
{
if (this->_grade == 1)
throw GradeTooHighException();
this->_grade--;
}
void Bureaucrat::decrement()
{
if (this->_grade == 150)
throw GradeTooLowException();
this->_grade++;
}
void Bureaucrat::signForm(Form& form) const
{
form.beSigned(*this);
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "Too small grade";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "Too big grade";
}
std::ostream& operator<<(std::ostream &stream, Bureaucrat &src)
{
stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl;
return stream;
}

45
ex01/src/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include <exception>
#include <string>
#include "Form.hpp"
class Form;
class Bureaucrat
{
private:
const std::string _name;
int _grade;
public:
Bureaucrat();
~Bureaucrat();
Bureaucrat(const Bureaucrat& src);
Bureaucrat(const std::string& name, int grade);
Bureaucrat& operator=(const Bureaucrat& src);
const std::string& getName() const;
int getGrade() const;
void setGrade(int new_grade);
void increment();
void decrement();
void signForm(Form& form) const;
class GradeTooHighException: public std::exception
{
public:
const char* what() const throw();
};
class GradeTooLowException: public std::exception
{
public:
const char* what() const throw();
};
};
std::ostream& operator<<(std::ostream& stream, Bureaucrat &src);

55
ex01/src/Form.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "Form.hpp"
#include <iostream>
#include <string>
Form::Form()
{
std::cout << "Form()" << std::endl;
}
Form::Form(const Form& src)
{
std::cout << "Form(Form)" << std::endl;
*this = src;
}
Form::Form(const std::string& name, bool signd, int to_execute, int to_sign):
_name(name),
_signed(signd),
_to_sign(to_sign),
_to_execute(to_execute)
{
std::cout << "Form(" << name << ", " << signd << ", " << to_execute << ", " << to_sign << ")" << std::endl;
if (to_execute > 150 || to_sign < 1)
throw GradeTooLowException();
if (to_execute < 1 || to_sign < 1)
throw GradeTooHighException();
}
Form::~Form()
{
std::cout << "~Form()" << std::endl;
}
Form& Form::operator=(const Form& src)
{
this->_signed = src._signed;
this->_to_sign = src._to_sign;
this->_to_execute = src._to_execute;
return *this;
}
void Form::beSigned(const Bureaucrat &bureaucrat)
{
if (this->_signed == false)
{
std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the form is already sign" << std::endl;
return;
}
if (bureaucrat.getGrade() > this->_to_sign)
throw GradeTooLowException();
this->_signed = true;
std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl;
}

42
ex01/src/Form.hpp Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <exception>
#include <ostream>
#include <string>
#include "Bureaucrat.hpp"
class Bureaucrat;
class Form
{
private:
const std::string _name;
bool _signed;
int _to_sign;
int _to_execute;
public:
Form();
Form(const std::string& name, bool signd, int to_execute, int to_sign);
Form(const Form& src);
~Form();
Form& operator=(const Form& src);
void beSigned(const Bureaucrat& bureaucrat);
class GradeTooHighException: public std::exception
{
public:
const char * what() const throw();
};
class GradeTooLowException: public std::exception
{
public:
const char * what() const throw();
};
};
std::ostream& operator<<(std::ostream& os, const Form& src);

94
ex01/src/main.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "Bureaucrat.hpp"
#include <iostream>
int main() {
std::cout << "Constructor too high grade" << std::endl;
{
try {
Bureaucrat crat("crat", 0);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "Constructor too low grade" << std::endl;
{
try {
Bureaucrat crat("crat", 151);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "increment too high" << std::endl;
{
Bureaucrat crat("crat", 1);
try {
crat.increment();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "decrement too low" << std::endl;
{
Bureaucrat crat("crat", 150);
try {
crat.decrement();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "setgrade too low" << std::endl;
{
Bureaucrat crat("crat", 42);
try {
crat.setGrade(151);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "setgrade too high" << std::endl;
{
Bureaucrat crat("crat", 42);
try {
crat.setGrade(0);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "increment normal" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << "default grade: " << crat.getGrade() << std::endl;
crat.increment();
std::cout << "after increment grade: " << crat.getGrade() << std::endl;
}
std::cout << std::endl;
std::cout << "decrement normal" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << "default grade: " << crat.getGrade() << std::endl;
crat.decrement();
std::cout << "after decrement grade: " << crat.getGrade() << std::endl;
}
std::cout << std::endl;
std::cout << "print" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << crat;
}
std::cout << std::endl;
}