Inherited FlashDrive The purpose of this assignment is to work with exceptions a
ID: 3667602 • Letter: I
Question
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 – drive1;
cout << "the other drives'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;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
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;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
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;
}
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;
}
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 – drive1;
cout << "the other drives'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;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
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;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
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;
}
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;
}
Explanation / Answer
OverFlowingFlashDriveException.h
#include <stdexcept>
#include <string>
using namespace std;
class OverFlowingFlashDriveException :public logic_error
{
public:
OverFlowingFlashDriveException(string message);
virtual ~OverFlowingFlashDriveException();
};
OverFlowingFlashDriveException::OverFlowingFlashDriveException(string message) :logic_error(message)
{
}
OverFlowingFlashDriveException::~OverFlowingFlashDriveException()
{
}
UnderFlowingFlashDriveException.h
#include<stdexcept>
#include<string>
using namespace std;
class UnderFlowingFlashDriveException:public logic_error
{
public:
UnderFlowingFlashDriveException(string message);
virtual ~UnderFlowingFlashDriveException();
};
UnderFlowingFlashDriveException::UnderFlowingFlashDriveException(string message) :logic_error(message)
{
}
UnderFlowingFlashDriveException::~UnderFlowingFlashDriveException()
{
}
FlashDrive.h
#include<iostream>
using namespace std;
class FlashDrive
{
public:
FlashDrive();
FlashDrive(int capacity, int used, bool pluggedIn);
~FlashDrive();
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 operator+(FlashDrive& drive);
FlashDrive operator-(FlashDrive& drive);
bool operator<(FlashDrive& drive);
bool operator>(FlashDrive& drive);
friend ostream& operator<<(ostream& os, const FlashDrive& drive);
friend istream &operator>>(istream &input, FlashDrive &drive);
private:
int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;
};
FlashDrive.cpp
#include "FlashDrive.h"
#include "OverFlowingFlashDriveException.h"
#include "UnderFlowingFlashDriveException.h"
FlashDrive::FlashDrive()
{
}
FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
my_StorageCapacity = capacity;
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
FlashDrive::~FlashDrive()
{
}
void FlashDrive::plugIn()
{
my_IsPluggedIn = true;
}
void FlashDrive::pullOut()
{
my_IsPluggedIn = false;
}
void FlashDrive::writeData(int amount)
{
if (isPluggedIn())
{
if (amount >= 0)
{
if ((my_StorageCapacity + amount) <= my_StorageCapacity)
{
setUsed(my_StorageUsed + amount);
}
else
{
throw OverFlowingFlashDriveException("drive does not have enough space to store this data");
}
}
else
{
throw UnderFlowingFlashDriveException("amount of data to be written on drive cannot be -ve");
}
}
else
{
cout << "First plugin the drive" << endl;
}
}
void FlashDrive::eraseData(int amount)
{
if (isPluggedIn())
{
if (amount >= 0)
{
if (my_StorageUsed - amount > 0)
{
setUsed(my_StorageUsed - amount);
}
else
{
throw OverFlowingFlashDriveException("Data stored in drive is less than the amount of data you are trying to erase");
}
}
else
{
throw UnderFlowingFlashDriveException("amount of data to be erased cannot be -ve");
}
}
}
void FlashDrive::formatDrive()
{
if (isPluggedIn())
{
my_StorageUsed = 0;
}
}
int FlashDrive::getCapacity()
{
return my_StorageCapacity;
}
void FlashDrive::setCapacity(int amount)
{
if (isPluggedIn())
{
if (amount >=0)
{
my_StorageCapacity = amount;
}
else
{
throw UnderFlowingFlashDriveException("capacity cannot be negative");
}
}
}
int FlashDrive::getUsed()
{
return my_StorageUsed;
}
void FlashDrive::setUsed(int amount)
{
if (isPluggedIn())
{
if (amount > 0)
{
if (amount > my_StorageCapacity)
{
my_StorageUsed = amount;
}
else
{
throw OverFlowingFlashDriveException("Amount of data you are trying to store is greater than capacity of drive");
}
}
else
{
throw UnderFlowingFlashDriveException("Usage of drive cannot be negative");
}
}
}
bool FlashDrive::isPluggedIn()
{
return my_IsPluggedIn;
}
FlashDrive FlashDrive::operator+(FlashDrive& drive)
{
FlashDrive fd;
if ((this->my_StorageUsed + drive.my_StorageUsed) > (this->my_StorageCapacity + drive.my_StorageCapacity))
{
throw OverFlowingFlashDriveException("Cannot combine the drives storage is greater than capacity");
}
fd.my_StorageCapacity = this->my_StorageCapacity + drive.my_StorageCapacity;
fd.my_StorageUsed = this->my_StorageUsed + drive.my_StorageUsed;
return fd;
}
FlashDrive FlashDrive::operator-(FlashDrive& drive)
{
FlashDrive fd;
if ((this->my_StorageCapacity - drive.my_StorageCapacity) < 0)
{
throw UnderFlowingFlashDriveException("Storage capacity of drive cannot be less than zero");
}
fd.my_StorageCapacity = this->my_StorageCapacity - drive.my_StorageCapacity;
if ((this->my_StorageUsed - drive.my_StorageUsed) > fd.my_StorageCapacity)
{
throw OverFlowingFlashDriveException("Data usage cannot exceed capacity");
}
fd.my_StorageUsed = this->my_StorageUsed - drive.my_StorageUsed;
return fd;
}
bool FlashDrive::operator<(FlashDrive& drive)
{
if (this->my_StorageCapacity < drive.my_StorageCapacity)
{
return true;
}
return false;
}
bool FlashDrive::operator>(FlashDrive& drive)
{
if (this->my_StorageCapacity > drive.my_StorageCapacity)
{
return true;
}
return false;
}
ostream& operator<<(ostream& os, const FlashDrive& drive)
{
os << "capacity " << drive.my_StorageCapacity << " used " << drive.my_StorageUsed << " plugged in " << drive.my_IsPluggedIn << endl;
return os;
}
istream &operator>>(istream &input, FlashDrive &drive)
{
input >> drive.my_StorageCapacity >> drive.my_StorageUsed >> drive.my_IsPluggedIn;
return input;
}