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

Pointer FlashDrive Using the FlashDrive class provided earlier ( VS2012 or XCode

ID: 3667597 • Letter: P

Question

Pointer FlashDrive

Using the FlashDrive class provided earlier ( VS2012 or XCode7), 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...

FlashDrive Class

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( );

Driver Code

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 * ptrDrive = new FlashDrive( 0, 0, false );
FlashDrive * nullDrive = NULL; // a pointer to anything can be NULL...

// let's work with the pointers...
cin >> ptrDrive;
cin >> nullDrive; // be careful...

cout << ptrDrive << endl;
cout << nullDrive << endl; // be careful...


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 (std::logic_error) {
cout << "Exception correctly caught..." << 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 (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}

try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}

FlashDrive Class

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;

Explanation / Answer

Note:

In the given code without changing method prototype, variable name and the given code , the solution is prepared.

Program:

FlashDrive.cpp

#include<iostream>

using namespace std;

class FlashDrive

{

public:

   // Method procol definition

   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 operator+(FlashDrive& drive);

   FlashDrive operator-(FlashDrive& drive);

   bool operator<(FlashDrive& drive);

   bool operator>(FlashDrive& drive);

   friend ostream& operator<<(ostream& outs, const FlashDrive& drive);

   friend istream &operator>>(istream &ins, FlashDrive& drive);

// variable declaration

private:

   int my_StorageCapacity;

   int my_StorageUsed;

   bool my_IsPluggedIn;

};

//constructor

FlashDrive::FlashDrive(){}

// constructor with argument

FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)

{

   my_StorageCapacity = capacity;

   my_StorageUsed = used;

   my_IsPluggedIn = pluggedIn;

}

// method to set drive is plugin

void FlashDrive::plugIn()

{

   my_IsPluggedIn = true;

}

// method to set drive is plugout

void FlashDrive::pullOut()

{

   my_IsPluggedIn = false;

}

// method to write data in drive

void FlashDrive::writeData(int amount)

{

   if (isPluggedIn())

   {

       if (amount >= 0)

       {

           if ((my_StorageCapacity + amount) <=

my_StorageCapacity)

           {

               setUsed(my_StorageUsed + amount);

           }

           else

           {

               throw logic_error("There is no enough space to

store data");

           }         

       }

       else

       {

           throw logic_error(" Data to be written on drive

cannot be negative");

       }

   }

   else

   {

       cout << "First plugin the drive" << endl;

   }

}

// method to delete drive data

void FlashDrive::eraseData(int amount)

{

   if (isPluggedIn())

   {

       if (amount >= 0)

       {

           if (my_StorageUsed - amount > 0)

           {

               setUsed(my_StorageUsed - amount);

           }

           else

           {

               throw logic_error("You are trying to erase less

than the amount of data stored in drive");

           }         

       }

       else

       {

           throw logic_error(" Data to be erased cannot be

negative");

       }

   }

}

//method to format the drive

void FlashDrive::formatDrive()

{

   if (isPluggedIn())

   {

       my_StorageUsed = 0;

   }

}

// method to get the drive capacity

int FlashDrive::getCapacity()

{

   return my_StorageCapacity;

}

// method to set capacity

void FlashDrive::setCapacity(int amount)

{

   if (isPluggedIn())

   {

       if (amount >=0)

       {

           my_StorageCapacity = amount;

       }

       else

       {

           throw logic_error("No negative capacity");

       }     

   }

}

// method to get usage size

int FlashDrive::getUsed()

{

   return my_StorageUsed;

}

// method to set drive usage

void FlashDrive::setUsed(int amount)

{

   if (isPluggedIn())

   {

       if (amount > 0)

       {

           if (amount > my_StorageCapacity)

           {

               my_StorageUsed = amount;

           }

           else

           {

               throw logic_error("Size of data is greater than

drive capacity");

           }

        }

       else

       {

           throw logic_error("Negative usage of drive");

       }

   }

}

// method to check drive is plugin

bool FlashDrive::isPluggedIn()

{

   return my_IsPluggedIn;

}

// method to perform operator over load +

FlashDrive FlashDrive::operator+(FlashDrive& drive)

{

   FlashDrive flsdrv;

   if ((this->my_StorageUsed + drive.my_StorageUsed) > (this->

   my_StorageCapacity + drive.my_StorageCapacity))

   {

       throw logic_error("Cannot combine the drives storage is

   greater than capacity");

   }

   flsdrv.my_StorageCapacity = this->my_StorageCapacity +

   drive.my_StorageCapacity;

   flsdrv.my_StorageUsed = this->my_StorageUsed +

   drive.my_StorageUsed;

   return flsdrv;

}

// method to perform operator over load -

FlashDrive FlashDrive::operator-(FlashDrive& drive)

{

   FlashDrive flsdrv;

   if ((this->my_StorageCapacity - drive.my_StorageCapacity)< 0)

   {

       throw logic_error("Storage capacity of drive cannot be

less than zero");

   }

   flsdrv.my_StorageCapacity = this->my_StorageCapacity –

   drive.my_StorageCapacity;

   if ((this->my_StorageUsed - drive.my_StorageUsed) >

   flsdrv.my_StorageCapacity)

   {

       throw logic_error("Data usage cannot exceed capacity");

   }

   flsdrv.my_StorageUsed = this->my_StorageUsed –

   drive.my_StorageUsed;

   return flsdrv;

}

// method to perform operator over load <

bool FlashDrive::operator<(FlashDrive& drive)

{

   if (this->my_StorageCapacity < drive.my_StorageCapacity)

   {

       return true;

   }

   return false;

}

// method to perform operator over load >

bool FlashDrive::operator>(FlashDrive& drive)

{

   if (this->my_StorageCapacity > drive.my_StorageCapacity)

   {

       return true;

   }

   return false;

}

// method to perform operator over load <<

ostream& operator << (ostream& outs, const FlashDrive& drive)

{

   outs << "capacity " << drive.my_StorageCapacity << " used "

<< drive.my_StorageUsed << " plugged in " <<

   drive.my_IsPluggedIn << endl;

   return outs;

}

// method to perform operator over load >>

istream &operator >> (istream &ins, FlashDrive &drive)

{

   ins >> drive.my_StorageCapacity >> drive.my_StorageUsed >> drive.my_IsPluggedIn;

   return ins;

}

Driver.cpp

#include <iostream>

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 * ptrDrive = new FlashDrive( 0, 0, false );

     FlashDrive * nullDrive = NULL; // a pointer to anything can

be NULL...

     // let's work with the pointers...

     cin >> ptrDrive;

     cin >> nullDrive; // be careful...

     cout << ptrDrive << endl;

     cout << nullDrive << endl; // be careful...

     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 (std::logic_error)

     {

     cout << "Exception correctly caught..." << 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 (std::logic_error le)

     {

     cout << "Exception correctly caught..." << le.what( ) <<

endl;

     }

     try

     {

     FlashDrive f( 10, 20, false );

     cout << "This should not print out..." << endl;

     } catch (std::logic_error)

     {

     cout << "Exception correctly caught..." << endl;

     }

}