Background: This assignment deals with inheritance. Inheritance is one of the ma
ID: 3661953 • 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( );
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
1. FlashDrive.h
------------------------------------
/*
* FlashDrive.h
*
* FlashDrive class
*
*/
#ifndef FLASHDRIVE_H_
#define FLASHDRIVE_H_
#include <iostream>
using namespace std;
class FlashDrive {
private:
int my_StorageCapacity; //capacity of drive
int my_StorageUsed; //storage used of drive
bool my_IsPluggedIn; //flag whether drive is plugged in
public:
//constructor
FlashDrive();
FlashDrive(int capacity, int used, bool pluggedIn);
//destructor
virtual ~FlashDrive();
//member functions
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( );
};
#endif /* FLASHDRIVE_H_ */
----------------------------------------
2. FlashDrive.cpp
-----------------------------------
/*
* FlashDrive.cpp
*
* Implementation of FlashDrive class
*
*/
#include "FlashDrive.h"
#include "OverFlowingFlashDriveException.h"
#include "UnderFlowingFlashDriveException.h"
/**
* Default constructor
*/
FlashDrive::FlashDrive() {
my_StorageCapacity=100; //default capacity
my_StorageUsed=0;
my_IsPluggedIn=false;
}
/*
* Parameterized constructor
*/
FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn )
{
if(used > capacity)
throw OverFlowingFlashDriveException();
my_StorageCapacity=capacity;
my_StorageUsed=used;
my_IsPluggedIn=pluggedIn;
}
/**
* Destructor
*/
FlashDrive::~FlashDrive() {
// TODO Auto-generated destructor stub
}
/**
* plugs in drive
*/
void FlashDrive::plugIn( )
{
my_IsPluggedIn=true;
}
/**
* pulls out drive
*/
void FlashDrive::pullOut( )
{
my_IsPluggedIn=false;
}
/**
* Write data to drive
*/
void FlashDrive::writeData(int amount)
{
if(amount < 0)
throw UnderFlowingFlashDriveException();
if(amount > my_StorageCapacity)
throw OverFlowingFlashDriveException();
cout<<"Data to be written to drive:"<<amount<<endl;
my_StorageUsed+=amount;
my_StorageCapacity-=amount;
}
/**
* Erase data from drive
*/
void FlashDrive::eraseData(int amount)
{
if(amount < 0)
throw UnderFlowingFlashDriveException();
if(amount > my_StorageCapacity)
throw OverFlowingFlashDriveException();
cout<<"Data to erased from drive:"<<amount<<endl;
my_StorageUsed-=amount;
my_StorageCapacity+=amount;
}
/**
* Formats drive
*/
void FlashDrive::formatDrive( )
{
cout<<"Drive formatted"<<endl;
my_StorageCapacity+=my_StorageUsed;
my_StorageUsed=0;
}
/**
* Returns capacity of drive
*/
int FlashDrive::getCapacity( )
{
return my_StorageCapacity;
}
/**
* Sets capacity of drive
*/
void FlashDrive::setCapacity(int amount)
{
if(amount < 0)
throw UnderFlowingFlashDriveException();
my_StorageCapacity=amount;
}
/**
* Returns storage used of drive
*/
int FlashDrive::getUsed( )
{
return my_StorageUsed;
}
/**
* Sets storage used
*/
void FlashDrive::setUsed(int amount)
{
if(amount < 0)
throw UnderFlowingFlashDriveException();
my_StorageUsed=amount;
}
/**
* Returns whether drive is plugged in.
*/
bool FlashDrive::isPluggedIn( )
{
return my_IsPluggedIn;
}
----------------------------------------------
3. OverFlowingFlashDriveException.h
-----------------------------------
/*
* OverFlowingFlashDriveException.h
*
* OverFlowingFlashDriveException class
*
*/
#ifndef OVERFLOWINGFLASHDRIVEEXCEPTION_H_
#define OVERFLOWINGFLASHDRIVEEXCEPTION_H_
#include <stdexcept>
using namespace std;
class OverFlowingFlashDriveException:public logic_error {
public:
OverFlowingFlashDriveException();
};
#endif /* OVERFLOWINGFLASHDRIVEEXCEPTION_H_ */
4. OverFlowingFlashDriveException.cpp
-------------------------------------
/*
* OverFlowingFlashDriveException.cpp
*
* Implementation of OverFlowFlashDriveException class
*
*/
#include "OverFlowingFlashDriveException.h"
OverFlowingFlashDriveException::OverFlowingFlashDriveException(): logic_error("OverFlowingFlashDriveException thrown!") {
}
------------------------------------
5. UnderFlowFlashDriveException.h
---------------------------------
/*
* UnderFlowingFlashDriveException.h
*
* UnderFlowingFlashDriveException class
*
*/
#ifndef UNDERFLOWINGFLASHDRIVEEXCEPTION_H_
#define UNDERFLOWINGFLASHDRIVEEXCEPTION_H_
#include <stdexcept>
using namespace std;
class UnderFlowingFlashDriveException:public logic_error {
public:
UnderFlowingFlashDriveException();
};
#endif /* UNDERFLOWINGFLASHDRIVEEXCEPTION_H_ */
-------------------------------------------
6. UnderFlowFlashDriveException.cpp
--------------------------------------
/*
* UnderFlowingFlashDriveException.cpp
*
* Implementation of UnderFlowingFlashDriveException class
*
*/
#include "UnderFlowingFlashDriveException.h"
UnderFlowingFlashDriveException::UnderFlowingFlashDriveException():logic_error("UnderflowingFlashDriveException thrown!") {
}
--------------------------------------------
6. main.cpp: main() function
------------------------------------------------
#include <iostream>
#include "FlashDrive.h"
#include "UnderFlowingFlashDriveException.h"
#include "OverFlowingFlashDriveException.h"
using namespace std;
int 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( );
// Let's try to get some exceptions...
try {
drive1.writeData(-1000);
cout << "This should not print out..." << endl;
} catch (UnderFlowingFlashDriveException& e) {
cout << e.what() << endl;
}catch(std::logic_error& e) {
cout << "This should not print out..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
}catch (UnderFlowingFlashDriveException& e) {
cout << e.what() << endl;
} catch (std::logic_error& e) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (UnderFlowingFlashDriveException& e) {
cout << e.what() << 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& e) {
cout << e.what() << endl;
} catch (std::logic_error& e) {
cout << "This should not print out..." << endl;
}
return 0;
}
-----------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.