add: main, << operator, name in const

This commit is contained in:
Camille Chauvet 2023-08-31 13:26:40 +00:00
parent 68acc04ee8
commit 02d0932e9d
3 changed files with 99 additions and 12 deletions

View File

@ -28,7 +28,6 @@ Bureaucrat::~Bureaucrat()
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
{
this->_name = src._name;
this->_grade = src._grade;
return *this;
@ -43,11 +42,6 @@ void Bureaucrat::setGrade(int new_grade)
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;
@ -82,8 +76,8 @@ const char* Bureaucrat::GradeTooHighException::what() const throw()
return "Too big grade";
}
std::ostream& Bureaucrat::operator<<(std::ostream &stream)
std::ostream& operator<<(std::ostream &stream, Bureaucrat &src)
{
stream << this->_name << ", bureaucrat grade " << this->_grade << "." << std::endl;
stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl;
return stream;
}

View File

@ -6,7 +6,7 @@
class Bureaucrat
{
private:
std::string _name;
const std::string _name;
int _grade;
public:
@ -19,14 +19,11 @@ class Bureaucrat
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:
@ -38,5 +35,7 @@ class Bureaucrat
public:
const char* what() const throw();
};
};
std::ostream& operator<<(std::ostream& stream, Bureaucrat &src);

94
ex00/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;
}