ex00_ok
This commit is contained in:
Executable
BIN
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!");
|
||||
}
|
||||
|
||||
+17
-3
@@ -18,9 +18,23 @@ class Bureaucrat
|
||||
~Bureaucrat();
|
||||
Bureaucrat &operator=(const Bureaucrat &other);
|
||||
|
||||
std::string get_name() const;
|
||||
int get_grade() const;
|
||||
|
||||
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,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);
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user