Background: This assignment deals with inheritance. Inheritance is one of the ma
ID: 3667313 • 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;
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
InheritedFlashDrive.cpp
/*
Purpose: 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.
*/
#include <iostream>
#include "InheritedFlashDrive.h"
#include <stdexcept> // supports Linux exceptions
#include "UnderflowingFlashDriveException.h"
#include "OverflowingFlashDriveException.h"
using namespace std;
FlashDrive::FlashDrive() {
my_StorageCapacity = 0;
my_StorageUsed = 0;
my_IsPluggedIn = false;
}
FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn) {
if (capacity >= 0 && used >= 0 && capacity >= used)
{
my_StorageCapacity = capacity;
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
else if (capacity < 0 || used < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (used > capacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
void FlashDrive::plugIn() {
my_IsPluggedIn = true;
}
void FlashDrive::pullOut() {
my_IsPluggedIn = false;
}
void FlashDrive::writeData(int amount) {
// throw exception if a negative number or having a value
// that exceeds my_StorageCapacity is potentially stored in my_StorageUsed
if (0 <= amount && (my_StorageUsed + amount) <= my_StorageCapacity)
{
my_StorageUsed += amount;
}
else if (my_StorageUsed + amount < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (my_StorageUsed + amount > my_StorageCapacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
void FlashDrive::eraseData(int amount) {
my_StorageUsed -= amount;
}
void FlashDrive::formatDrive() {
my_StorageUsed = 0;
}
int FlashDrive::getCapacity() {
return(my_StorageCapacity);
}
void FlashDrive::setCapacity(int amount) {
// throw exception if a negative number is potentially
// stored in myStorageCapacity
if (amount >= 0)
{
my_StorageCapacity = amount;
}
else if ( amount < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (amount > my_StorageCapacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
int FlashDrive::getUsed() {
return(my_StorageUsed);
}
void FlashDrive::setUsed(int amount) {
// throw exception if a negative number or having a value
// that exceeds myStorageCapacity is potentially stored in myStorageUsed
if (0 <= amount && amount <= my_StorageCapacity)
{
my_StorageUsed = amount;
}
else if (amount < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (amount > my_StorageCapacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
bool FlashDrive::isPluggedIn() {
return(my_IsPluggedIn);
}
/**
* @Overload '+' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (add-up values of their capacities; used capacities, sets
* it to the unplugged condition). Throw exception if a negative number or
* value that exceeds myStorageCapacity is potentially stored in
* my_StorageCapacity or setUsed.
*/
FlashDrive operator + (const FlashDrive& drive1, const FlashDrive& drive2) {
int new_my_StorageCapacity = drive1.my_StorageCapacity +
drive2.my_StorageCapacity;
int new_my_StorageUsed = drive1.my_StorageUsed + drive2.my_StorageUsed;
// here, it is enough to check that capacity is not less than the used
// space, the rest will be catched in setCapacity and setUsed.
if (new_my_StorageCapacity >= new_my_StorageUsed)
{
FlashDrive newFlashDrive = FlashDrive();
newFlashDrive.setCapacity(new_my_StorageCapacity);
newFlashDrive.setUsed(new_my_StorageUsed);
// or FlashDrive newFlashDrive = FlashDrive(new_my_StorageCapacity,
// new_my_StorageUsed, false);
// return the created FlashDrive
return newFlashDrive;
}
else if (new_my_StorageCapacity < 0 || new_my_StorageUsed < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (new_my_StorageUsed > new_my_StorageCapacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
/**
* @Overload '-' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (subtract values of their capacities; used capacities, sets
* it to the unplugged condition). Throw exception if a negative number or
* value that exceeds myStorageCapacity is potentially stored in
* my_StorageCapacity or setUsed.
*/
FlashDrive operator - (const FlashDrive& drive1, const FlashDrive& drive2) {
int new_my_StorageCapacity = drive1.my_StorageCapacity -
drive2.my_StorageCapacity;
int new_my_StorageUsed = drive1.my_StorageUsed - drive2.my_StorageUsed;
// here, it is enough to check that capacity is not less than the used
// space, the rest will be catched in setCapacity and setUsed.
if (new_my_StorageCapacity >= new_my_StorageUsed)
{
FlashDrive newFlashDrive = FlashDrive();
newFlashDrive.setCapacity(new_my_StorageCapacity);
newFlashDrive.setUsed(new_my_StorageUsed);
// or FlashDrive newFlashDrive = FlashDrive(new_my_StorageCapacity,
// new_my_StorageUsed, false);
// return the created FlashDrive
return newFlashDrive;
}
else if (new_my_StorageCapacity < 0 || new_my_StorageUsed < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (new_my_StorageUsed > new_my_StorageCapacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
/**
* @Overload '<' operator.
* Returns true if the amount of free space on drive1 is less than on drive2,
* false otherwise.
*/
bool operator < (const FlashDrive& drive1, const FlashDrive& drive2) {
return (drive1.my_StorageCapacity - drive1.my_StorageUsed <
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity < drive2.my_StorageCapacity);
}
/**
* @Overload '>' operator.
* Returns true if the amount of free space on drive1 is bigger than on drive2,
* false otherwise.
*/
bool operator > (const FlashDrive& drive1, const FlashDrive& drive2) {
return (drive1.my_StorageCapacity - drive1.my_StorageUsed >
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity > drive2.my_StorageCapacity);
}
/**
* @Overload '<<' operator.
* Prints out values of the private member variables of the FlashDrive class.
* Uses iostream:
*/
ostream & operator << (ostream& outs, const FlashDrive& drive) {
outs << "Capacity: " << drive.my_StorageCapacity << " kilobytes; " << endl;
outs << "Used space: " << drive.my_StorageUsed << " kilobytes; " << endl;
outs << "Free space: " << (drive.my_StorageCapacity - drive.my_StorageUsed)
<< " kilobytes; " << endl;
outs << "The flash drive is "
<< (drive.my_IsPluggedIn == true ? "plugged-in. " : "unplugged. ");
return outs;
}
/**
* @Overload '>>' operator.
* Reads-in and sets the values of the private member variables of
* the FlashDrive class.
* Uses iostream:
*/
istream & operator >> (istream& ins, FlashDrive& drive) {
// Prompt the user to enter the capacity of the drive.
int capacity;
cout << "Enter the capacity of the flash drive in kilobytes ";
cout << "(positive integer): ";
ins >> capacity;
// Prompt the user to enter the used capacity of the drive.
int usedCapacity;
cout << "Enter the used capacity of the flash drive in kilobytes ";
cout << "(non-negative integer): ";
ins >> usedCapacity;
// Prompt the user to enter the state of the drive.
bool isPluggedIn;
do
{
cout << "Is it plugged-in [1=yes/0=no]?: ";
ins >> isPluggedIn;
if (isPluggedIn == 0 || isPluggedIn == 1)
{
break;
}
ins.clear();
} while (true);
// Assign the entered values to the private variables of FlashDrive.
if (capacity >= usedCapacity)
{
drive.setCapacity(capacity);
drive.setUsed(usedCapacity);
(isPluggedIn == 1) ? drive.plugIn() : drive.pullOut();
// return the created FlashDrive
return ins;
}
else if (capacity < 0 || usedCapacity < 0)
{
//cerr << "Error in writeData..." << endl;
throw UnderflowingFlashDriveException();
}
else if (usedCapacity > capacity)
{
//cerr << "Error in writeData..." << endl;
throw OverflowingFlashDriveException();
}
else
{
//cerr << "Error in FlashDrive..." << endl;
throw logic_error("Error: Invalid data!");
}
}
InheritedFlashDrive.h
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_H
#include <iostream>
using namespace std;
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 FlashDrive operator + (const FlashDrive & drive1, const FlashDrive & drive2);
friend FlashDrive operator - (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator < (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator > (const FlashDrive & drive1, const FlashDrive & drive2);
friend ostream & operator << (ostream & outs, const FlashDrive & drive);
friend istream & operator >> (istream & ins, FlashDrive & drive);
private:
int my_StorageCapacity; // in kilobytes
int my_StorageUsed; // in kilobytes
bool my_IsPluggedIn; // am I attached to a computer?
};
#endif
main.cpp
#include <iostream>
#include "InheritedFlashDrive.h"
#include <stdexcept> // supports Linux exceptions
#include "UnderflowingFlashDriveException.h"
#include "OverflowingFlashDriveException.h"
using namespace std;
void main()
{
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;
}
catch (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;
}
catch (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;
}
}
OverflowingFlashDriveException.cpp
#include <iostream>
#include "InheritedFlashDrive.h"
#include <stdexcept> // supports Linux exceptions
#include "UnderflowingFlashDriveException.h"
#include "OverflowingFlashDriveException.h"
using namespace std;
void main()
{
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;
}
catch (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;
}
catch (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;
}
}
OverflowingFlashDriveException.h
#ifndef OVERFLOWINGFLASHDRIVEEXCEPTION_H
#define OVERFLOWINGFLASHDRIVEEXCEPTION_H
#include <iostream>
using namespace std;
class OverflowingFlashDriveException : public logic_error
{
public:
OverflowingFlashDriveException();
private:
};
#endif
UnderflowingFlashDriveException.cpp
#include <iostream>
#include "UnderflowingFlashDriveException.h"
#include <exception>
using namespace std;
UnderflowingFlashDriveException::UnderflowingFlashDriveException()
:logic_error("UnderflowingFlashDriveException Logic Error")
{
//constructor can be modified
}
UnderflowingFlashDriveException.h
#ifndef UNDERFLOWINGFLASHDRIVEEXCEPTION_H
#define UNDERFLOWINGFLASHDRIVEEXCEPTION_H
#include <iostream>
using namespace std;
class UnderflowingFlashDriveException : public logic_error
{
public:
UnderflowingFlashDriveException();
private:
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.