Using the FlashDrive class provided earlier, upgrade the class so that it throws
ID: 3540599 • Letter: U
Question
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 FlashDrivewith 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.
HINT: Recall that you can create a logic_error by passing a string message. For example,
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.
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( );
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 %u2013 drive1;
cout << "the other cup'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 %u2013 combined;
cout << "something not right here..." << endl;
} catch( UnderflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( OverflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( UnderflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;
drive3 = &drive2;
cout << drive3 << endl;
drive3 = new FlashDrive();
cin >> drive3;
cout << drive3 << endl;
delete( drive3 );
}
Implementation Details Sample Driver Code FlashDrive 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; #include <iostream>
#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 %u2013 drive1;
cout << "the other cup'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 %u2013 combined;
cout << "something not right here..." << endl;
} catch( UnderflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( OverflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( UnderflowingFlashDriveException ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;
drive3 = &drive2;
cout << drive3 << endl;
drive3 = new FlashDrive();
cin >> drive3;
cout << drive3 << endl;
delete( drive3 );
}
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 FlashDrivewith 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.Explanation / Answer
#include<iostream>
#include <stdexcept>
using namespace std;
class UnderflowingFlashDriveException: public logic_error
{
public:
UnderflowingFlashDriveException(string error):logic_error(error)
{
}
};
class OverflowingFlashDriveException: public logic_error
{
public:
OverflowingFlashDriveException(string error):logic_error(error)
{
}
};
namespace cs52 {
class FlashDrive
{
public:
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( );
friend istream& operator>>(istream& in,FlashDrive& F);
friend istream& operator>>(istream& in,FlashDrive* F);
friend ostream& operator<<(ostream& out,FlashDrive& F);
friend ostream& operator<<(ostream& out,FlashDrive* F);
friend FlashDrive operator+(const FlashDrive &f1, const FlashDrive &f2);
friend FlashDrive operator-(const FlashDrive &f1, const FlashDrive &f2);
bool operator>(const FlashDrive& F);
bool operator<(const FlashDrive& F);
private:
int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;
};
FlashDrive::FlashDrive()
{
my_StorageCapacity = 0;
my_StorageUsed = 0;
my_IsPluggedIn = false;
}
FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn )
{
if(capacity < 0 ) throw UnderflowingFlashDriveException("Invalid Capacity,Cant create Drive.");
my_StorageCapacity = capacity;
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
void FlashDrive::plugIn( )
{
if(!my_IsPluggedIn) my_IsPluggedIn = true;
}
void FlashDrive::pullOut( )
{
if(my_IsPluggedIn) my_IsPluggedIn = false;
}
void FlashDrive::writeData( int amount )
{
if(amount > my_StorageCapacity) throw OverflowingFlashDriveException("Trying to write more than its capacity, not possible.");
my_StorageUsed = my_StorageUsed+amount;
}
void FlashDrive::eraseData( int amount )
{
my_StorageUsed = my_StorageUsed-amount;
}
void FlashDrive::formatDrive( )
{
my_StorageUsed = 0;
}
int FlashDrive::getCapacity( )
{
return my_StorageCapacity;
}
void FlashDrive::setCapacity( int amount )
{
my_StorageCapacity = amount;
}
int FlashDrive::getUsed( )
{
return my_StorageUsed;
}
void FlashDrive::setUsed( int amount )
{
my_StorageUsed = amount;
}
bool FlashDrive::isPluggedIn( )
{
return my_IsPluggedIn;
}
istream& operator>>(istream& in,FlashDrive& F)
{
in >> F.my_StorageCapacity;
in >> F.my_StorageUsed;
in >> F.my_IsPluggedIn;
return in;
}
istream& operator>>(istream& in,FlashDrive* F)
{
in >> F->my_StorageCapacity;
in >> F->my_StorageUsed;
in >> F->my_IsPluggedIn;
return in;
}
ostream& operator<<(ostream& out,FlashDrive& F)
{
out << "Storage capacity is " << F.my_StorageCapacity << " used is " << F.my_StorageUsed <<endl; return out;
}
ostream& operator<<(ostream& out,FlashDrive* F)
{
if(F!=NULL)
out << "Storage capacity is " << F->my_StorageCapacity << " used is " << F->my_StorageUsed <<endl;
else
out << "Invalid Drive";
return out;
}
FlashDrive operator+(const FlashDrive &f1, const FlashDrive &f2)
{
return FlashDrive(f1.my_StorageCapacity+f2.my_StorageCapacity,f1.my_StorageUsed+f2.my_StorageUsed,false);
}
FlashDrive operator-(const FlashDrive &f1, const FlashDrive &f2)
{
if(f1.my_StorageUsed ==0) throw UnderflowingFlashDriveException("no space to use");
return FlashDrive(f1.my_StorageCapacity+f2.my_StorageCapacity,f1.my_StorageUsed-f2.my_StorageUsed,false);
}
bool FlashDrive::operator>(const FlashDrive& F)
{
return (this->my_StorageUsed > F.my_StorageUsed);
}
bool FlashDrive::operator<(const FlashDrive& F)
{
return (this->my_StorageUsed < F.my_StorageUsed);
}
}
int 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 cup'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( UnderflowingFlashDriveException UE) {
cout << "exception caught: " << UE.what() << ' ';
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( OverflowingFlashDriveException OE) {
cout << "exception caught: " << OE.what() << ' ';
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( UnderflowingFlashDriveException UE) {
cout << "exception caught: " << UE.what() << ' ';
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
} catch( std::logic_error ) {
// the catch above should have caught this error, not this one!
cout << "something not right here..." << endl;
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;
drive3 = &drive2;
cout << drive3 << endl;
drive3 = new FlashDrive();
cin >> drive3;
cout << drive3 << endl;
delete( drive3 );
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.