Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background: This assignment deals with inheritance. Inheritance is one of the ma

ID: 3662028 • Letter: B

Question

Background:

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 13: 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;
cin >> "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 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;

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;
cin >> "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

#pragma once
#include <stdexcept>
#include <string>
using namespace std;
class OverFlowingFlashDriveException :public logic_error
{
public:
   OverFlowingFlashDriveException(string message);
   virtual ~OverFlowingFlashDriveException();
};

OverFlowingFlashDriveException.cpp

#include "stdafx.h"
#include "OverFlowingFlashDriveException.h"


OverFlowingFlashDriveException::OverFlowingFlashDriveException(string message) :logic_error(message)
{

}

OverFlowingFlashDriveException::~OverFlowingFlashDriveException()
{
}

UnderFlowingFlashDriveException.h

#pragma once
#include <stdexcept>
#include <string>
using namespace std;
class UnderFlowingFlashDriveException :public logic_error
{
public:
UnderFlowingFlashDriveException(string message);
   virtual ~UnderFlowingFlashDriveException();
};

UnderFlowingFlashDriveException.cpp

#include "stdafx.h"
#include "UnderFlowingFlashDriveException.h"


UnderFlowingFlashDriveException::UnderFlowingFlashDriveException(string message) :logic_error(message)
{

}

UnderFlowingFlashDriveException::~UnderFlowingFlashDriveException()
{
}

FlashDrive.cpp(Implemented FlashDrive.cpp only for modules which are declared in FlashDrive.h i.e skipped the operator overloading portion as it was not there in FlashDrive.h)

#include "stdafx.h"
#include "FlashDrive.h"


FlashDrive::FlashDrive()
{
}

FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
   if (capacity < 0)
   {
       throw UnderFlowingFlashDriveException("Capacity of drive cannot be negative");
   }
   my_StorageCapacity = capacity;

   if (used < 0)
   {
       throw UnderFlowingFlashDriveException("Usage of drive cannot be less than zero");
   }
   else if (used > my_StorageCapacity)
   {
       throw OverFlowingFlashDriveException("Usage of drive exceeded the max capacity of flash drive");
   }
   else
   {
       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)
       {
           setUsed(my_StorageUsed + amount);
       }
       else
       {
           throw UnderFlowingFlashDriveException("negative amount of data cannot be written on the drive");
       }
   }
   else
   {
       cout << "First plugin the drive" << endl;
   }

}
void FlashDrive::eraseData(int amount)
{
   if (isPluggedIn())
   {
       if (amount >= 0)
       {
           setUsed(my_StorageUsed - amount);
       }
       else
       {
           throw UnderFlowingFlashDriveException("negative amount of data cannot be erased from the drive");
       }
   }

}

void FlashDrive::formatDrive()
{
   if (isPluggedIn())
   {
       my_StorageUsed = 0;
   }
}

int FlashDrive::getCapacity()
{
   return my_StorageCapacity;
}

void FlashDrive::setCapacity(int amount)
{
   if (isPluggedIn)
   {
       if (amount < 0)
       {
           throw UnderFlowingFlashDriveException("Capacity of drive cannot be negative");
       }
       my_StorageCapacity = amount;
   }  
}

int FlashDrive::getUsed()
{
   return my_StorageUsed;
}

void FlashDrive::setUsed(int amount)
{
   if (isPluggedIn())
   {
       if (amount < 0)
       {
           throw UnderFlowingFlashDriveException("Usage of drive cannot be less than zero");
       }
       if (amount > my_StorageCapacity)
       {
           throw OverFlowingFlashDriveException("Usage of drive exceeded the max capacity of flash drive");
       }

       my_StorageUsed = amount;
   }
}

bool FlashDrive::isPluggedIn()
{
   return my_IsPluggedIn;
}