Question
Please help me finish the rest of my code by subclassing std::logic_error . In each exceptional situation , rather than writing errors to cout or
Please help me finish the rest of my code by subclassing std::logic_error. In each exceptional situation, rather than writing errors to cout or throwing std::logic_error, please create and throw a custom subclass as shown below:
my code(main.cpp, flashdrive_3_0.h, flashdrive_3_0.cpp):
flashdrive_3_0.cpp
#include "flashdrive_3_0.h" // allows for separate compilation if you want. #include using namespace std;
namespace cs52/amespace cs52.
{ FlashDrive::FlashDrive( ) { my_StorageCapacity = 0; my_StorageUsed = 0; my_IsPluggedIn = false; }
FlashDrive::FlashDrive( int capacity, int used, bool pluggedIn )//constructor for class flashdrive with parameters. { //similar to 2.0 then add 3 logic_error if(capacity
if(used
if(used > capacity) //logic_error u bigger than c throw logic_error("---Error---used can not be more than capacity----Error---");
my_StorageCapacity = capacity; //parameters(capacity) for Storage space. my_StorageUsed = used; //parameters(used) for Storage space can be used my_IsPluggedIn = pluggedIn; //parameters(pluggedIn) }
void FlashDrive::plugIn( ) { my_IsPluggedIn = true; //plugIn set my_IsPluggedIn T. }
void FlashDrive::pullOut( ) { my_IsPluggedIn = false; //pullOut set my_IsPluggedIn F. }
void FlashDrive::writeData( int amount ) //parameters( amount) in write date to the storage. { if(amount
if(my_StorageUsed + amount > my_StorageCapacity){ my_StorageUsed += 0; //set my_StorageUsed to 0, so that the program can keep running. throw logic_error("---Error---Can't fit the capacity-------Error---");//Error status---NOT ENOUGH SPACE }
my_StorageUsed += amount;//If successful, add amount to storage. }
void FlashDrive::eraseData( int amount )//parameters( amount) in erase date from the storage. { if(amount
void FlashDrive::formatDrive( ) //format. { if(!isPluggedIn()) throw logic_error("---Error---flash drive is not plugged in to format----Error---"); my_StorageUsed = 0; //Used should be 0.
}
//--accessors int FlashDrive::getCapacity( ) //return Capacity { return my_StorageCapacity; }
void FlashDrive::setCapacity( int amount )//f_D_2_0 +2 logic_error { if(amount
my_StorageCapacity = amount; }
//--accessors int FlashDrive::getUsed( ) //return Used { return my_StorageUsed; }
void FlashDrive::setUsed( int amount )//f_D_2_0 +2 logic_error { if(amount my_StorageCapacity) throw logic_error(" -------Can't fit the capacity----------- ");//show error message. my_StorageUsed = amount; }
bool FlashDrive::isPluggedIn( ) //To check PluggedIn. { return my_IsPluggedIn; }
//Enhance the FlashDrive class so that it supports the operators (+,-,). FlashDrive FlashDrive:: operator +(const FlashDrive& other) { cs52::FlashDrive operatorFlashDrive;//temp drive operatorFlashDrive.setCapacity(my_StorageCapacity + other.my_StorageCapacity);//Capacity-Capacity operatorFlashDrive.setUsed(my_StorageUsed + other.my_StorageUsed);//Used-Used return operatorFlashDrive;//return value }
FlashDrive FlashDrive::operator -(const FlashDrive& other) { if(other.my_StorageCapacity > my_StorageCapacity) throw logic_error("---Error---other's capacity > myStorageCapacity.---------Error---");
if(other.my_StorageUsed > my_StorageUsed) throw logic_error("---Error---other's Used > myStorageUsed. can not subtract----Error---");
cs52::FlashDrive operatorFlashDrive;//temp drive operatorFlashDrive.setCapacity(my_StorageCapacity - other.my_StorageCapacity);//Capacity-Capacity operatorFlashDrive.setUsed(my_StorageUsed - other.my_StorageUsed);//Used-Used return operatorFlashDrive;//return value
}
bool FlashDrive::operator
bool FlashDrive::operator >(const FlashDrive& other)//comapre { return my_StorageCapacity > other.my_StorageCapacity;//return value }
ostream& operator
istream& operator >> (istream& in, FlashDrive& fd)//overload { in >> fd.my_StorageCapacity >> fd.my_StorageUsed; return in; } }
flashdrive_3_0.h
#ifndef FlashDrive_3_0_h // Avoid redeclaring class FlashDrive. #define FlashDrive_3_0_h // This code is compiled only once #include using std::ostream; using std::istream;
////class FlashDrive defintion//// namespace cs52 { class FlashDrive { public: // class member functions. FlashDrive( ); //This is a default constructor. FlashDrive( int capacity, int used, bool pluggedIn );
void plugIn( ); void pullOut( ); void writeData( int amount ); void eraseData( int amount ); void formatDrive( );
int getCapacity( ); //Gets the amount of space can be used. void setCapacity( int amount );
int getUsed( ); //Gets the amount of space used. void setUsed( int amount );
bool isPluggedIn( ); //Test PluggedIn.
//Enhance the FlashDrive class so that it supports the operators (+,-,). FlashDrive operator +(const FlashDrive &other); //operator + FlashDrive operator -(const FlashDrive &other); //operator - bool operator (const FlashDrive& other); //operator >
friend ostream& operator > (istream& in, FlashDrive& fd);//overload
private: int my_StorageCapacity; //int Capacity. int my_StorageUsed; //int used. bool my_IsPluggedIn;//Parameterized constructor. }; } #endif // FLASHDRIVE_3_0_H
main.cpp
#include #include //for getch #include "FlashDrive_3_0.h"//input header for flashdriver. using namespace std;
int main( ) { using namespace cs52; cs52::FlashDrive empty; cs52::FlashDrive drive1( 10, 0, false );//(capacity, used,pluggedIn). cs52::FlashDrive drive2( 20, 0, false );
//driver 1 info drive1.plugIn( ); drive1.formatDrive( ); drive1.writeData( 5 ); drive1.pullOut( );
//driver 2 info 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; cout > sample;
// print out a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be printed cout
cs52::FlashDrive combined = drive1 + drive2; cout
cs52::FlashDrive other = combined - drive1; cout other){ cout
if (drive2 > other) { cout
// let's throw some exceptions... try { empty = empty - combined; cout
try{ drive2.writeData( 10000 ); cout
try{ cs52::FlashDrive f( -1, -1, false ); cout
//pause program and show Press any key to continue// std::cout
MY run:
New Require:
main.cpp
flashdrive_4_0.h
flashdrive_4_0.cpp
Enter data for a flash drive: (capacity, used) 10 12 capacity 10, used: 12, plugged in: O this drive's filled to 6 the other cup's filled to 1 looks 1ike combined is bigger. looks ike other is bigger.. . looks ike drivel is smaller.. Try Try2 ---Errorflash drive is not plugged in to write data--Error--- Try3 -Error--capacity can not be negative number- ---Error- Enter data for a flash drive: (capacity, used) 10 12 capacity 10, used: 12, plugged in: O this drive's filled to 6 the other cup's filled to 1 looks 1ike combined is bigger. looks ike other is bigger.. . looks ike drivel is smaller.. Try Try2 ---Errorflash drive is not plugged in to write data--Error--- Try3 -Error--capacity can not be negative number- ---Error
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started