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

Directions: Please complete the following assignment to signify your completion

ID: 3835352 • Letter: D

Question

Directions:
Please complete the following assignment to signify your completion of Unit 17. All programming projects need to be completed and submitted electronically, following the Electronic Submission Guidelines discussed in class. Please cherry-pick from your solution folder and submit just the .cpp files you created as well as .exe file that Visual Studio built from your code.

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 16: Inherited FlashDrive
Using the FlashDrive class provided earlier, upgrade the class so that it throws subclasses of the exception class std::logic_error when the user does silly things. In the past, about all you could do when the user demanded silly operations was use cout to state your displeasure over making an overflowing FlashDrive (one where its contents exceeded its size) or an underflowing FlashDrive (one with a negative contents values). In the past, you blindly used std::logic_error. However now that you have seen inheritance, Id like you to make your FlashDrive class throw more specific exceptions at various times. For example, if while using operator-, you end up with a FlashDrive with a negative contents, throw a UnderflowingFlashDriveException back at the user. If while using operator+, you end up with a FlashDrive with more contents than its size allows, throw a OverflowingFlashDriveException at the user. A revised sample driver is shown below. Throw the right kind of subclasses exception anytime your user makes silly demands on you.

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.

Following the diagrams show below, create the class UnderflowingFlashDriveException and OverflowingFlashDriveException. Like other exception subclasses seen in class, these classes don't need any methods or members at all - just a public constructor and to inherit from std::logic_error.

FURTHER HINT: Please scroll down to the end to see the changes to the driver code made for Unit 17 purposes...

Driver Code

Sample Output

Project 2: Musician

A Musician represents a player of some kind of instrument. The subclass Pianist represents a piano player whose instrument is a piano. Define these two classes so that they have the behavior shown in the sample driver and the sample output below.

Implementation Details

Sample Driver

Musician

Musician( std::string instrument );
std::string getInstrument( ) const;
virtual void play( );

std::string myInstrument;

Pianist : public Musician

Pianist( );
virtual void play( );

Pianist * ptrP = new Pianist( );
cout << "ptrP->getInstrument()" << endl;
cout << ptrP->getInstrument( ) << endl; // piano
cout << "ptrP->play()" << endl;
ptrP->play();


Musician m;
Pianist p;
m = p;      /// legal
/// p = m; /// illegal!

Musician * temp;
cout << "temp pointing at a musician..." << endl;
temp = ptrM;
cout << "temp->play()" << endl;
temp->play( );
cout << "temp pointing at a pianist..." << endl;
temp = ptrP;
cout << "temp->play()" << endl;
temp->play( );

Sample Output

PLEASE DON'T SEND ME THE ENTIRE VISUAL STUDIO SOLUTION FOLDER. Instead, as you'll do with every homework assignment, please cherry-pick from your hard-drive. Select just the source (.cpp) file and the executable (.exe) file.

Windows users will find the executable file (known as an Application) in your Solution Folder's Debug/Release folder. Mac users will find the executable file listed under the Products folder of their XCode solution. Mac users should click the arrow associated with the "Full Path" in the right-hand side of the screen under "Identity and Type" to navigate to the folder where the executable file resides.

Copy these two files together into a folder named with your name and student ID. From Windows Explorer, select this folder and then right-click and select Send to->Compressed (zipped) Folder to create a .zip file containing this folder. From MacOS, select this folder and then select File->Compress "FolderName" to create a .zip file containing this folder. Then upload this file into the submission area below by clicking Submit Assignment. Then click on the Choose File button to select this file. Click on Submit Assignment to finalize this file submission. All these steps are described in the Electronic Submission Guidelines in the General top section of class.

Driver Code

  #include <iostream>  
  #include "OverflowingFlashDriveException.h"  
  #include "UnderflowingFlashDriveException.h"  
  #include "FlashDrive.h"  
  void main( )  {  using namespace cs52;  cs52::FlashDrive empty;  cs52::FlashDrive drive1( 10, 0, false );  cs52::FlashDrive drive2( 20, 0, false );    drive1.plugIn( );  drive1.formatDrive( );  drive1.writeData( 5 );  drive1.pullOut( );    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;  cin >> sample;    // print out a FlashDrive...  // the class designer for FlashDrive (that's you!)  // gets to decide which fields matter and should be printed  cout << sample << endl;    cs52::FlashDrive combined = drive1 + drive2;  cout << "this drive's filled to " << combined.getUsed( ) << endl;    cs52::FlashDrive other = combined – drive1;  cout << "the other cup'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;  }  
  // let's throw some exceptions...  
  try {    empty = empty – combined;    cout << "something not right here..." << endl;  } catch( UnderflowingFlashDriveException ) {  // an exception should get thrown...   // so the lines of code here should  // be run, not the cout statement...  } catch( std::logic_error ) {  // the catch above should have caught this error, not this one!    cout << "something not right here..." << endl;  }  
  try {    drive2.writeData( 10000 );    cout << "something not right here..." << endl;  } catch( OverflowingFlashDriveException ) {  // an exception should get thrown...   // so the lines of code here should  // be run, not the cout statement...  } catch( std::logic_error ) {  // the catch above should have caught this error, not this one!    cout << "something not right here..." << endl;  }    try {    cs52::FlashDrive f( -1, -1, false );    cout << "something not right here..." << endl;  } catch( UnderflowingFlashDriveException ) {  // an exception should get thrown...   // so the lines of code here should  // be run, not the cout statement...  } catch( std::logic_error ) {  // the catch above should have caught this error, not this one!    cout << "something not right here..." << endl;  }  
  // work with the new stuff added for Unit 16!!!  
  cs52::FlashDrive * drive3 = NULL;  // careful...  cout << drive3 << endl;  
  drive3 = &drive2;  cout << drive3 << endl;  
  drive3 = new FlashDrive();  cin  >> drive3;  cout << drive3 << endl;  
  delete( drive3 );  
  }  

Sample Output

----some tests follow----
A can with a size of 200 containing 8 items
A can with a size of 10 containing 0 items
> is working...
< is working...
== is working...
---broken code follow---
got an exception...
got an exception...

Explanation / Answer

#include #include "OverflowingFlashDriveException.h" #include "UnderflowingFlashDriveException.h" #include "FlashDrive.h" void main( ) { using namespace cs52; cs52::FlashDrive empty; cs52::FlashDrive drive1( 10, 0, false ); cs52::FlashDrive drive2( 20, 0, false ); drive1.plugIn( ); drive1.formatDrive( ); drive1.writeData( 5 ); drive1.pullOut( ); 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; cin >> sample; // print out a FlashDrive... // the class designer for FlashDrive (that's you!) // gets to decide which fields matter and should be printed cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote