Add ostream operator overload for Bureaucrat and clean up main function

This commit is contained in:
whaffman 2025-06-24 00:02:40 +02:00
parent 5410b6228c
commit 37300e08a6
3 changed files with 14 additions and 2 deletions

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <exception> #include <exception>
#include <iostream>
#define HIGHEST_GRADE 1 #define HIGHEST_GRADE 1
#define LOWEST_GRADE 150 #define LOWEST_GRADE 150
@ -38,3 +39,5 @@ public:
const char *what() const throw(); const char *what() const throw();
}; };
}; };
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat);

View File

@ -87,4 +87,10 @@ const char * Bureaucrat::GradeTooHighException::what() const throw()
const char * Bureaucrat::GradeTooLowException::what() const throw() const char * Bureaucrat::GradeTooLowException::what() const throw()
{ {
return "Grade too low!"; return "Grade too low!";
} }
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
std::cout << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
return os;
}

View File

@ -15,6 +15,7 @@ int main (void)
{ {
std::cerr << "Caught exception: " << e.what() << std::endl; std::cerr << "Caught exception: " << e.what() << std::endl;
} }
try try
{ {
Bureaucrat alice("alice", 151); // This should also throw an exception Bureaucrat alice("alice", 151); // This should also throw an exception
@ -23,6 +24,7 @@ int main (void)
{ {
std::cerr << "Caught exception: " << e.what() << std::endl; std::cerr << "Caught exception: " << e.what() << std::endl;
} }
try try
{ {
Bureaucrat charlie("charlie", 1); Bureaucrat charlie("charlie", 1);
@ -32,6 +34,7 @@ int main (void)
{ {
std::cerr << "Caught exception: " << e.what() << std::endl; std::cerr << "Caught exception: " << e.what() << std::endl;
} }
std::cout << jan << std::endl;
std::cout << copy << std::endl;
} }