The purpose of this assignment is to work with exceptions. As you may recall, I
ID: 3835387 • Letter: T
Question
The purpose of this assignment is to work with exceptions. As you may recall, I have provided you with a sample class named FlashDrive 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 <stdexcept> to begin working with logic_error, but Visual Studio (being a badly behaved child...) let's you get away without it.
Although the sample driver code might not code for all these circumstances, I would like you to throw exceptions when:
more stuff has been put onto the drive than it can safely hold (due to writeData)
a negative number is potentially used as a my_StorageUsed value (due to operator – or bad values being sent to the constructor call)
a negative number is potentially used as a my_StorageCapacity value (due to operator – 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.
I'd also like you to get operator << and operator >> working for the class FlashDrive. And finally, I'd like you to place FlashDrive into the namespace cs52.
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 <stdexcept> when working with this class. Linux fans will require this include; its optional for Windows users but won't hurt anything if you do it. Here is a class diagram for logic_error. As I said, it already exists so please make use of it.
Sample Driver#include <iostream>
#include <stdexcept> // for logic_error #include "FlashDrive.h
void main( ) { using namespace cs52; cs52::FlashDrive empty; cs52::FlashDrive drive1( 10, 0, false ); cs52::FlashDrive drive2( 20, 0, false ); drive1.plugIn( ); drive1.formatDrive( ); drive1.writeData( 5 ); drive1.pullOut( ); drive2.plugIn( ); drive2.formatDrive( ); drive2.writeData( 1 ); drive2.pullOut( ); // read in a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be read in cs52::FlashDrive sample; cin >> sample; // print out a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be printed cout << sample << endl; cs52::FlashDrive combined = drive1 + drive2; cout << "this drive's filled to " << combined.getUsed( ) << endl; cs52::FlashDrive other = combined – drive1; cout << "the other drive's filled to " << other.getUsed( ) << endl; if (combined > other) { cout << "looks like combined is bigger..." << endl; } else { cout << "looks like other is bigger..." << endl; } if (drive2 > other) { cout << "looks like drive2 is bigger..." << endl; } else { cout << "looks like other is bigger..." << endl; } if (drive2 < drive1) { cout << "looks like drive2 is smaller..." << endl; } else { cout << "looks like drive1 is smaller..." << endl; } // let's throw some exceptions...
try { empty = empty – combined; cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { drive2.writeData( 10000 ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { cs52::FlashDrive f( -1, -1, false ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } }
Explanation / Answer
#include <iostream>
#include <stdexcept> // for logic_error
#include "FlashDrive.h
void main( )
{
using namespace cs52;
cs52::FlashDrive empty;
cs52::FlashDrive drive1( 10, 0, false );
cs52::FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
// read in a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be read in
cs52::FlashDrive sample;
cin >> sample;
// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
cs52::FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
cs52::FlashDrive other = combined – drive1;
cout << "the other drive's filled to " << other.getUsed( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 > other) {
cout << "looks like drive2 is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 < drive1) {
cout << "looks like drive2 is smaller..." << endl;
}
else {
cout << "looks like drive1 is smaller..." << endl;
}
// let's throw some exceptions...
try {
empty = empty – combined;
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.