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

Using FlashDrive.cpp ( VS2010 or VS2012 or XCode5 or XCode6 or XCode7 ), enhance

ID: 3662045 • Letter: U

Question

Using FlashDrive.cpp ( VS2010 or VS2012 or XCode5 or XCode6 or XCode7 ), enhance the FlashDrive class so that it supports the operators +, -, < and >. A sample pile of driver code is shown below to assist you in this effort. Operators + and - should create a new FlashDrive from the two arguments by combining their contents. If you wind up with a FlashDrive with a value stored that exceeds its capacity, print out an error message. If you wind up with a negative capacity or storage value, print out an error message.   Operators < and > must return bool and should compare the holdings of the two arguments to determine which one is bigger. Operator << should print out the values of the private member variables. Operator >> should read in and set the values of the private member variables.

My strong advice is to work one operator at a time, as these steps are very error-prone and lead to many, many compile errors.

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

#include <iostream>
#include "FlashDrive.h"
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 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;

}

FlashDrive Sample Driver Code FlashDrive

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;

#include <iostream>
#include "FlashDrive.h"
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 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;

}

Explanation / Answer

I have implemented the operator overloading in FlashDrive.

FlashDrive.h

#pragma once

#include<iostream>
using namespace std;
class FlashDrive
{
public:
   FlashDrive();
   FlashDrive(int capacity, int used, bool pluggedIn);
   ~FlashDrive();
   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& os, const FlashDrive& drive);
   friend istream &operator>>(istream &input, FlashDrive &drive);
private:
   int my_StorageCapacity;
   int my_StorageUsed;
   bool my_IsPluggedIn;
};

FlashDrive.cpp

#include "stdafx.h"
#include "FlashDrive.h"


FlashDrive::FlashDrive()
{
}

FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
   my_StorageCapacity = capacity;
   my_StorageUsed = used;
   my_IsPluggedIn = pluggedIn;
}

FlashDrive::~FlashDrive()
{
}

void FlashDrive::plugIn()
{
   my_IsPluggedIn = true;
}
void FlashDrive::pullOut()
{
   my_IsPluggedIn = false;
}
void FlashDrive::writeData(int amount)
{
   if (isPluggedIn())
   {
       if (amount >= 0)
       {
           setUsed(my_StorageUsed + amount);
       }
   }
   else
   {
       cout << "First plugin the drive" << endl;
   }

}
void FlashDrive::eraseData(int amount)
{
   if (isPluggedIn())
   {
       if (amount >= 0)
       {
           setUsed(my_StorageUsed - amount);
       }
   }

}

void FlashDrive::formatDrive()
{
   if (isPluggedIn())
   {
       my_StorageUsed = 0;
   }
}

int FlashDrive::getCapacity()
{
   return my_StorageCapacity;
}

void FlashDrive::setCapacity(int amount)
{
   if (isPluggedIn())
   {
       my_StorageCapacity = amount;
   }
}

int FlashDrive::getUsed()
{
   return my_StorageUsed;
}

void FlashDrive::setUsed(int amount)
{
   if (isPluggedIn())
   {
       my_StorageUsed = amount;
   }
}

bool FlashDrive::isPluggedIn()
{
   return my_IsPluggedIn;
}

FlashDrive FlashDrive::operator+(FlashDrive& drive)
{
   FlashDrive fd;
   if ((this->my_StorageUsed + drive.my_StorageUsed) > (this->my_StorageCapacity + drive.my_StorageCapacity))
   {
       cout << "Cannot combine the drives storage is greater than capacity" << endl;
       return fd;
   }
   fd.my_StorageCapacity = this->my_StorageCapacity + drive.my_StorageCapacity;
   fd.my_StorageUsed = this->my_StorageUsed + drive.my_StorageUsed;
   return fd;
}

FlashDrive FlashDrive::operator-(FlashDrive& drive)
{
   FlashDrive fd;
   if ((this->my_StorageCapacity - drive.my_StorageCapacity) < 0)
   {
       cout << "Storage capacity of drive cannot be less than zero" << endl;
       return fd;
   }
   fd.my_StorageCapacity = this->my_StorageCapacity - drive.my_StorageCapacity;
   if ((this->my_StorageUsed - drive.my_StorageUsed) > fd.my_StorageCapacity)
   {
       cout << "Data usage cannot exceed capacity" << endl;
       return fd;
   }
   fd.my_StorageUsed = this->my_StorageUsed - drive.my_StorageUsed;
   return fd;
}

bool FlashDrive::operator<(FlashDrive& drive)
{
   if (this->my_StorageCapacity < drive.my_StorageCapacity)
   {
       return true;
   }
   return false;
}

bool FlashDrive::operator>(FlashDrive& drive)
{
   if (this->my_StorageCapacity > drive.my_StorageCapacity)
   {
       return true;
   }
   return false;
}

ostream& operator<<(ostream& os, const FlashDrive& drive)
{
   os << "capacity " << drive.my_StorageCapacity << " used " << drive.my_StorageUsed << " plugged in " << drive.my_IsPluggedIn << endl;
   return os;
}
istream &operator>>(istream &input, FlashDrive &drive)
{
   input >> drive.my_StorageCapacity >> drive.my_StorageUsed >> drive.my_IsPluggedIn;
   return input;
}

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