Project 1: Inherited FlashDrive The purpose of this assignment is to work with e
ID: 662586 • Letter: P
Question
Project 1: Inherited FlashDrive
The purpose of this assignment is to work with exceptions and inheritance. Enhance the FlashDrive class so that the operators and other functions of the class throw subclassed exceptions when things go wrong, rather than just print out an error message. For example, if you wind up with a FlashDrive with a used value that exceeds its capacity, throw a OverflowingFlashDriveException, rather than just a "normal" std::logic_error. If you wind up with a negative used or capacity value, throw a UnderflowingFlashDrive Exception, rather than just a "normal" std::logic_error.
Please be sure that your subclasses call their parent class constructors.
I'd like you to enhance this class so that invoking its methods or operators potentially throw a custom exception, rather than just a std::logic_error. As you may recall, you can create a logic_error by passing a string value to its constructor. You say #include <stdexcept> to begin working with logic_error.
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 wont hurt anything if you do it.
FlashDrive( );
FlashDrive( int capacity, int used, bool pluggedIn );
void plugIn( );
void pullOut( );
void writeData( int amount );
void eraseData( int amount );
void formatDrive( );
int getCapacity( );
void setCapacity( int amount );
int getUsed( );
void setUsed( int amount );
bool isPluggedIn( );
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined
FlashDrive( );
FlashDrive( int capacity, int used, bool pluggedIn );
void plugIn( );
void pullOut( );
void writeData( int amount );
void eraseData( int amount );
void formatDrive( );
int getCapacity( );
void setCapacity( int amount );
int getUsed( );
void setUsed( int amount );
bool isPluggedIn( );
int my_StorageCapacity;int my_StorageUsed;
bool my_IsPluggedIn;
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined
Explanation / Answer
Answer
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (UnderflowingFlashDriveException) {
cout << "Exception correctly caught..." << endl;
} std::logic_error) {
cout << "This should not print out..." << endl;
}
Since , it negative -1000
it raise exception:
Exception correctly caught...
--------------------------------------------------------------
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
Exception: Exception correctly caught..
------------------------------------------------------------------------
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (UnderflowingFlashDriveException ) {
cout << "Exception correctly caught..." << endl;
} std::logic_error le) {
cout << "This should not print out..." << le.what( ) << endl;
}
Exception : Exception correctly caught...
----------------------------------------------------------------------------------------
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (OverflowingFlashDriveException) {
cout << "Exception correctly caught..." << endl;
} catch (std::logic_error) {
cout << "This should not print out..." << endl;
}
Exception
catch (std::logic_error) {
cout << "This should not print out..." << endl;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.