Question: PLEASE INCLUDE ALL .CPP AND .H FILES This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one
PLEASE INCLUDE ALL .CPP AND .H FILES
This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works before you begin.
Project 16: Inherited FlashDrive Using the FlashDrive class provided earlier, upgrade the class so that it throws subclasses of the exception class std::logic_error when the user does silly things. In the past, about all you could do when the user demanded silly operations was use cout to state your displeasure over making an overflowing FlashDrive (one where its contents exceeded its size) or an underflowing FlashDrive (one with a negative contents values). In the past, you blindly used std::logic_error. However now that you have seen inheritance, Id like you to make your FlashDrive class throw more specific exceptions at various times. For example, if while using operator-, you end up with a FlashDrive with a negative contents, throw a UnderflowingFlashDriveException back at the user. If while using operator+, you end up with a FlashDrive with more contents than its size allows, throw a OverflowingFlashDriveException at the user. A revised sample driver is shown below. Throw the right kind of subclasses exception anytime your user makes silly demands on you.
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
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.
| std::logic_error |
| logic_error( std::string message ); std::string what(); |
| std::string message; |
Following the diagrams show below, create the class UnderflowingFlashDriveException and OverflowingFlashDriveException. Like other exception subclasses seen in class, these classes don't need any methods or members at all - just a public constructor and to inherit from std::logic_error.
| Driver Code |
|
#include #include "OverflowingFlashDriveException.h" #include "UnderflowingFlashDriveException.h" #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 Output |
| ----some tests follow---- A can with a size of 200 containing 8 items A can with a size of 10 containing 0 items > is working... |
std: :logic error UnderflowingFlashDriveException OverflowingFlashDriveException
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
