Using FlashDrive.cpp ( VS2010 or VS2012 or XCode5 or XCode6 or XCode7 ), enhance
ID: 3661949 • 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 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
// ******************** *******************//
//Creating FlashDrive Class
//*************************************//
class FlashDrive
{
// integer varaibles
int capacity,used,amount,my_StorageCapacity,my_StorageUsed;
// boolean varaibles return true or false
bool my_IsPluggedIn;
public:
FlashDrive();// constructor
FlashDrive(capacity,used,my_IsPluggedIn);//parameter constructor
// methods
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( );
// adding of two objects values of FlashDrive class
FlashDrive add(FlashDrive drive1,FlashDrive drive2);
//Subtracting of two objects values from FlashDrive class
FlashDrive sub(FlashDrive drive1,FlashDrive drive2);
};
//Default Constructor
FlashDrive::FlashDrive(void){
cout<<"Flash Drive Object Created";
}
//parameter constructor
FlashDrive::FlashDrive(int capacity,int used, bool my_IsPluggedIn)
{
capacity = capacity;
used = used;
my_IsPluggedIn = my_IsPluggedIn;
}
void FlashDrive::WriteData(int number){
number =number;
}
void FlashDrive::setCapacity(int amount){
amount = amount;
}
int FlashDrive::getCapacity(){
return capacity;
}
void FlashDrive::setUsed(int used){
used = used;
}
int FlashDrive::getUsed(){
return used;
}
bool FlashDrive::isPluggedIn(bool my_IsPluggedIn;){
my_IsPluggedIn = my_IsPluggedIn;
}
// addition of two FlashDrive objects
FlashDrive::add(FlashDrive drive1,FlashDrive drive2){
//Creating drive3 object for addition of 2 drive objects
FlashDrive drive3 ;
drive3.getCapacity = drive1.getCapacity() + drive2.getCapacity();
drive3.getUsed = drive1.getUsed() + drive2.getUsed();
cout<<"FlashDrive 3 Objects<<" "<<drive3.getCapacity <<" "<<drive3.getUsed;
}
FlashDrive::sub(FlashDrive drive1,FlashDrive drive2){
//Creating drive3 object for subtraction of 2 drive objects
FlashDrive drive3 ;
drive3.getCapacity = drive1.getCapacity() -drive2.getCapacity();
drive3.getUsed = drive1.getUsed() - drive2.getUsed();
cout<<"FlashDrive 3 Objects<<" "<<drive3.getCapacity <<" "<<drive3.getUsed;
}
#include <iostream>
using namespace std;
void main( )
{
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.writeData( 5 );
drive2.plugIn( );
drive2.writeData( 1 );
FlashDrive combined = add(drive1,drive2);
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = sub(drive1,drive);
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.