init ex00

This commit is contained in:
Camille Chauvet 2023-08-30 15:47:10 +00:00
commit 68acc04ee8
3 changed files with 157 additions and 0 deletions

26
ex00/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

89
ex00/src/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,89 @@
#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->_name = src._name;
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;
}
void Bureaucrat::setName(const std::string &new_name)
{
this->_name = new_name;
}
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++;
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "Too small grade";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "Too big grade";
}
std::ostream& Bureaucrat::operator<<(std::ostream &stream)
{
stream << this->_name << ", bureaucrat grade " << this->_grade << "." << std::endl;
return stream;
}

42
ex00/src/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <exception>
#include <string>
class Bureaucrat
{
private:
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 setName(const std::string& new_name);
void setGrade(int new_grade);
void increment();
void decrement();
std::ostream& operator<<(std::ostream& stream);
class GradeTooHighException: public std::exception
{
public:
const char* what() const throw();
};
class GradeTooLowException: public std::exception
{
public:
const char* what() const throw();
};
};