Project 9: Fuller FlashDrive Using FlashDrive.cpp ( VS2010 or VS2012 or XCode5 o
ID: 3665772 • Letter: P
Question
Project 9: Fuller FlashDrive
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 FlashDriveFlashDrive( );
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
#include <iostream>
using namespace std;
class FlashDrive
{
private:
int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;
public:
FlashDrive( )
{
my_StorageCapacity=0;
my_StorageUsed = 0;
my_IsPluggedIn = false;
}
FlashDrive( int capacity, int used, bool pluggedIn )
{
if(capacity<0)
{
cout<<"capacoty can't be negetive, setting it to zero"<<endl;
capacity =0;
}
my_StorageCapacity=capacity;
if(used < 0)
{
cout<<"used can't be negetive, setting it to zero"<<endl;
used =0;
}
if(used > capacity)
{
cout<<"used can't be greaterthan capacity, setting it to capacity"<<endl;
used = capacity;
}
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
void plugIn( )
{
my_IsPluggedIn = true;
}
void pullOut( )
{
my_IsPluggedIn = false;
}
void writeData( int amount )
{
if(amount+my_StorageUsed > my_StorageCapacity)
{
cout<<"used can't be greaterthan capacity, setting it to capacity"<<endl;
amount = my_StorageCapacity;
}
else
my_StorageUsed = my_StorageUsed + amount;
}
void eraseData( int amount )
{
if(amount >my_StorageUsed)
{
cout<<"used can't be negetive, setting it to zero"<<endl;
my_StorageUsed =0;
}
else
my_StorageUsed = my_StorageUsed - amount;
}
void formatDrive( )
{
my_StorageUsed = 0;
}
int getCapacity( )
{
return my_StorageCapacity;
}
void setCapacity( int amount )
{
if(amount<0)
{
cout<<"capacoty can't be negetive, setting it to zero"<<endl;
amount =0;
}
my_StorageCapacity = amount;
}
int getUsed( )
{
return my_StorageUsed;
}
void setUsed( int amount )
{
if(amount < 0)
{
cout<<"used can't be negetive, setting it to zero"<<endl;
amount =0;
}
if(amount > my_StorageCapacity)
{
cout<<"used can't be greaterthan capacity, setting it to capacity"<<endl;
amount = my_StorageCapacity;
}
my_StorageUsed = amount;
}
bool isPluggedIn( )
{
return my_IsPluggedIn;
}
FlashDrive operator+ ( FlashDrive f)
{
FlashDrive f1;
f1.setCapacity(my_StorageCapacity+f.getCapacity());
f1.setUsed(my_StorageUsed + f.getUsed());
return f1;
}
FlashDrive operator- ( FlashDrive f)
{
FlashDrive f1;
f1.setCapacity(my_StorageCapacity-f.getCapacity());
f1.setUsed(my_StorageUsed - f.getUsed());
return f1;
}
bool operator< ( FlashDrive f)
{
return my_StorageCapacity < f.getCapacity();
}
bool operator> ( FlashDrive f)
{
return my_StorageCapacity > f.getCapacity();
}
friend ostream& operator<< (ostream& out,FlashDrive f)
{
out << "capacity = " << f.getCapacity() << ", used storage " << f.getUsed() << ", ";
if(f.isPluggedIn())
out<<" Flash drive is plugged in"<<endl;
else
out<<" Flash drive is not plugged in"<<endl;
return out;
}
friend istream& operator>>(istream& in, FlashDrive f)
{
int cap;
int used;
// cout<<"enter capacity"<<endl;
in>>cap;
f.setCapacity(cap);
// cout<<"used space"<<endl;
in>>used;
f.setUsed(used);
return in;
}
};
int 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;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.