Question

The purpose of this assignment is to work with exceptions. As you
may recall, I have provided you with a sample class named TrashCan
which has been diagrammed below. I'd like you to enhance this class
so that invoking its methods or operators potentially throw
exceptions, rather than just printing error messages to cout.
Currently, our favorite exception class is std::logic_error. You
can create a logic_error by passing a string value to its
constructor. Officially, you should also say #include to begin
working with logic_error, but Visual Studio (being a badly behaved
child...) let's you get away with it. Although the sample driver
code might not code for all these circumstances, I would like you
to throw exceptions whenever: a negative number is potentially
stored in myContents (due to calls to the constructor or operator
-) a negative number is potentially stored in mySize (due to calls
to the constructors or operator - or setSize) having a myContents
that exceeds mySize (due to calls to operator + or addItem or
setSize) So carefully wind your way thru all the operators and
methods of the class ensuring that logic_error gets thrown in each
of these circumstances. I would also like you to put TrashCan into
namespace cs52. You will notice the driver code has been updated as
if this has occurred. Now, your past code will no longer work right
out of the box until you make this change. Implementation Details
TrashCan TrashCan(); TrashCan( int size ); TrashCan( int size, int
contents ); void setSize( intsize ); voidaddItem(); void empty( );
void cover( ); void uncover( ); void printCan( ); bool myIsCovered;
intmy_Size; intmy_Contents; Sample Driver cs52::TrashCan t;
cs52::TrashCan yourCan( 128 ); cs52::TrashCan junk( 12 );
t.setSize( 3 ); t.addItem(); t.addItem(); t.addItem(); t.cover();
/// how can you have negative number of items /// TrashCan??? try {
junk = yourCan - t; cout << junk; } catch( std::logic_error
le ) { cout << "subtraction failed" << endl; } /// how
can a contents of 4 items in a TrashCan /// which has only has size
3??/ try { t.addItem( ); } catch( std::logic_error le ) { cout
<< "addItem failed" << endl; } //// t should be
unchanged cout << t << endl; /// how can you have
TrashCans with a negative /// size?? try { TrashCan myCan( -19 ); }
catch( std::logic_error le ) { cout << "constructor failed!"
<< endl; }