Question: This assignment revisits the issue of operator overloading. In this assignment, you are asked to implement < < and >>. These operators are fairly tricky

This assignment revisits the issue of operator overloading. In this assignment, you are asked to implement << and >>. These operators are fairly tricky because their parameters are unusual and the way you need to code them is not exactly straightforward. Please read over this issue in the book and review the online content so it is clear to you before you begin. In addition, this assignment starts dealing with pointers to classes. Students find pointers very challenging because code that uses pointer doesn't always work. It depends on whether the pointer is any good, pointing to a non-NULL value. If you choose to continue in the C++ track, you'll need to be very comfortable with pointers.

Project 15: Pointer FlashDrive

Using the FlashDrive class provided earlier, upgrade the class so that operator << and operator >> work well with pointers (that is, FlashDrive *). Youll need to re-overload these operators, adding the function:

friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive ); friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive ); 

HINT: Be very careful to test for NULL...

Driver Code

#include  #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 drive'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( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { drive2.writeData( 10000 ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } try { cs52::FlashDrive f( -1, -1, false ); cout << "something not right here..." << endl; } catch( std::logic_error ) { // an exception should get thrown... // so the lines of code here should // be run, not the cout statement... } 
// 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 ); 
} 

Please include both the header file and the cpp file for a rating!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!