C Exception Handling Sample Driver TrashCan yours TrashCan m

C++ Exception Handling

Sample Driver:

TrashCan yours;
TrashCan mine;
TrashCan test;

yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );

yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );


cout << test << endl;

try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << \"subtraction failed\" << endl;
}


/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << \"constructor failed\" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << \"setSize failed\" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << \"constructor failed\" << endl;
}

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. l\'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 as the contents value (due to calls to operator- or negative values being sent to the constructor call) a negative number is potentially stored as the size value (due to calls to operator - or setSize) having a contents value that exceeds the size value (due to calls to operatoror setSize or bad values being sent to the constructor call) 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. HINT: Recall that you can create a logic_error by passing a string message. For example, std::logic_error error \"Bad News\" ); While not required with Visual Studio, please #include when working with this class. Linux fans will require this include; its optional for Windows users but wont hurt anything if you do it. Here is a class diagram for logic_error. As l said, it already exists so please make use of it. std::logic error logic error std:.string message ) St.:string what , std:string message;

Solution

Follwoing is requried code

TrashCan (int size)
   {
       if(size < 0) {
           throw logic_error( \"Constructor (size) :Size value is negative\");
       }
       else{
           if (myContents > size)
           {
               throw logic_error( \"Constructor (size) :myContents value exceeds size value\");
           }
           mySize = size;
       }
   }
   TrashCan (int size, int contents)
   {
       if(size < 0) {
           throw logic_error( \"Constructor (size, content) : Size value is negative\");
       }
       else{
           mySize = size;
       }
       if(contents < 0 ) {
           throw logic_error( \"Constructor (size, content) :Content value is negative\");
       }
       else {
           if (contents > size) {
               throw logic_error( \"Constructor (size, content) :Content value exceeds Size value\");
           }
           else{
               myContents = contents;
           }
       }
   }

   TrashCan operator+(const TrashCan& b){
       TrashCan tc;
       tc.mySize = this->mySize + b.mySize;
       tc.myContents = this->myContents - b.myContents;
       if(tc.myContents > tc.mySize){
           throw logic_error( \"+ve Operator: Content size exceeds size value\" );
       }
       else{
           return tc;
       }
   }
   TrashCan operator-(const TrashCan& b){
       TrashCan tc;
       tc.mySize = this->mySize - b.mySize;
       if(tc.mySize < 0){
           throw logic_error( \"-ve Operator: size become negative after subtraction\" );
       }
       else
       {
           tc.myContents = this->myContents - b.myContents;
           if(tc.myContents < 0){
               throw logic_error( \"-ve Operator: Contents become negative after subtraction\" );
           }
           else{
               return tc;
           }
       }
   }

   void setSize(int amount)
   {
       if (amount < 0 ){
           throw logic_error( \"setSize: size become negative after subtraction\" );
       }
       else {
           if(amount < myContents){
               throw logic_error( \"setSize: Content size exceeds size value\" );
           }
           this->mySize = amount;
       }
   }

C++ Exception Handling Sample Driver: TrashCan yours; TrashCan mine; TrashCan test; yours.setSize( 10 ); mine.setSize( 10 ); test.setSize( 5 ); yours.addItem( )
C++ Exception Handling Sample Driver: TrashCan yours; TrashCan mine; TrashCan test; yours.setSize( 10 ); mine.setSize( 10 ); test.setSize( 5 ); yours.addItem( )
C++ Exception Handling Sample Driver: TrashCan yours; TrashCan mine; TrashCan test; yours.setSize( 10 ); mine.setSize( 10 ); test.setSize( 5 ); yours.addItem( )

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site