Question

I have provided you with a sample class named FlashDrive which has been diagrammed below. Using the FlashDrive class provided earlier, upgrade the class so that it supports various operators. Make operator+ combine together the contents of two FlashDrive, as long as the contents does not exceed the size. Make operator- subtract one FlashDrive contents from another, as long as the size or contents don't go negative. Support the >> and << operators to allow instances to be read from cin or written to cout. Make the boolean operators <, >, <= and >= test FlashDrive's contents. Make the boolean operator == and != test all of the data members of a TrashCan for an exact match.

Sample Driver Code ainclude kio stream #include FlashDrive. h using namespace std; void main FlashDrive drive1 10, 0, false FlashDrive drive20 20, e, false drive plugIn FlashDrive drivel formatDrive drivel writeData 5); FlashDrive FlashDrive( int capacity, int used, bool pluggedln drivel pullout drive 2. plugin void plug In(); drive2.formatDrive void pullout drive2.write Data 1); void write Data( int amount); drive 2.pullout void erase Data( int amount): FlashDrive combined drivel drive2; void formatDrive(); this drives filled to combined. getused cout int get Capacity(): endl void setCapacity int amount). FlashDrive other int get Used(): cout the other drives filled to other getused void set Used( int amount endl if (combined other) int my StorageCapacity; cout looks like combined is bigger endl; int my StorageUsed: bool my lsPluggedIn else cout looks like other is bigger endl if (drive2 other f looks like drive2 is bigger endl cout else looks like other is bigger endl cout if (drive 2 drive1 cout looks like drive2 is smaller endl else cout looks like drive1 is smaller endl;

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//FlashDrive header file declaration
//FlashDrive.h
#ifndef FLASDRIVE_H
#define FLASDRIVE_H
#include<iostream>
using namespace std;
class FlashDrive
{
public:
   FlashDrive();
   FlashDrive(int capacity, int used, bool pluggedIn);

   void plugIn();
   void pullOut();

   void writeData(int amount);
   void eraseData(int amount);

   void formatDrive();
   int getCapacity();

   int getUsed();
   void setUsed(int amount);

   bool isPluggedIn();


   //declaratio of overload +,-
   friend FlashDrive operator+(FlashDrive &f1,FlashDrive &f2);
   friend FlashDrive operator-(FlashDrive &f1,FlashDrive &f2);

   //declaratio of overload <,<=
   friend bool operator<(FlashDrive &f1,FlashDrive &f2);
   friend bool operator<=(FlashDrive &f1,FlashDrive &f2);

   //declaratio of overload >,>=
   friend bool operator>(FlashDrive &f1,FlashDrive &f2);
   friend bool operator>=(FlashDrive &f1,FlashDrive &f2);

   //declaratio of overload ==, !=
   friend bool operator==(FlashDrive &f1,FlashDrive &f2);
   friend bool operator!=(FlashDrive &f1,FlashDrive &f2);

   //declaratio of overload <<,>>
   friend ostream& operator<<(ostream&out, FlashDrive &f);
   friend istream& operator>>(istream&out, FlashDrive &f);


private:
   int my_StorageCapacity;
   int my_StorageUsed;
   bool my_IsPluggedIn;
};

#endif FLASDRIVE_H

--------------------------------------------------------------------------------------------------------

//implemenation of FlashDrive.cpp
//FlashDrive.cpp
#include "FlashDrive.h"
FlashDrive::FlashDrive()
{
   my_StorageCapacity=0;
   my_StorageUsed=0;
   my_IsPluggedIn=false;
}
FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
   my_StorageCapacity=capacity;
   my_StorageUsed=used;
   my_IsPluggedIn=pluggedIn;
}

void FlashDrive::plugIn()
{
   my_IsPluggedIn=true;
}
void FlashDrive::pullOut()
{
   my_IsPluggedIn=false;
}

void FlashDrive::writeData(int amount)
{
   my_StorageUsed+=amount;
}
void FlashDrive::eraseData(int amount)
{
   my_StorageUsed-=amount;
}

void FlashDrive::formatDrive()
{
   my_StorageUsed=0;
}
int FlashDrive::getCapacity()
{
   return my_StorageCapacity;
}

int FlashDrive::getUsed()
{
   return my_StorageUsed;
}
void FlashDrive::setUsed(int amount)
{
   this->my_StorageUsed=amount;
}

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

FlashDrive operator+(FlashDrive &f1,FlashDrive &f2)
{
   FlashDrive combined;

   combined.my_StorageCapacity=f1.getCapacity()+f2.getCapacity();
   combined.my_StorageUsed=f1.getUsed()+f2.getUsed();

   return combined;
}
FlashDrive operator-(FlashDrive &f1,FlashDrive &f2)
{
   FlashDrive combined;

   combined.my_StorageCapacity=f1.getCapacity()-f2.getCapacity();
   combined.my_StorageUsed=f1.getUsed()-f2.getUsed();

   return combined;
}

bool operator<(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() < f2.getCapacity()
       && f1.getUsed() < f2.getUsed();
}
bool operator>(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() > f2.getCapacity()
       && f1.getUsed() > f2.getUsed();
}

bool operator<=(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() <= f2.getCapacity()
       && f1.getUsed() <= f2.getUsed();
}
bool operator>=(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() >= f2.getCapacity()
       && f1.getUsed() >= f2.getUsed();
}

bool operator==(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() == f2.getCapacity()
       && f1.getUsed() == f2.getUsed()
       && f1.isPluggedIn() ==f2.isPluggedIn();
}
bool operator!=(FlashDrive &f1,FlashDrive &f2)
{
   return f1.getCapacity() != f2.getCapacity()
       && f1.getUsed() != f2.getUsed()
       && f1.isPluggedIn() !=f2.isPluggedIn();
}
//overloaiding << operator
ostream & operator << (ostream &out, FlashDrive &f)
{
   out << "Capacity : "<<f.my_StorageCapacity
   << "Used : " << f.my_StorageUsed
   << "Plugged In : " << f.my_IsPluggedIn <<endl;

    return out;
}
//overloaiding >> operator
istream & operator >> (istream &in, FlashDrive &f)
{
    cout << "Enter capacity";
   in >> f.my_StorageCapacity;
    cout << "Enter used used ";
   in >> f.my_StorageUsed;

    return in;
}

--------------------------------------------------------------------------------------------------------

//Test cpp file for FlashDrive class
//Main.cpp
#include<iostream>
//include header file
#include "FlashDrive.h"
using namespace std;
//main method
int main()
{

   //Create an object of FlashDrive
   FlashDrive drive1(10,0,false);
   FlashDrive drive2(20,0,false);

   //calling methods
   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<<"this other drive's filled to "
       <<combined.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 bigger..."          
           <<endl;
   }
   else{
       cout<<"looks like drive1 is bigger..."          
           <<endl;
   }

   system("pause");
   return 0;
}

--------------------------------------------------------------------------------------------------------

Sampel result :

this drives filled to 6 this other drives filled to 6 oOKS like combined 1s bigger... looks 1ike other is bigger.. . bire... looks like drive1 is

Add a comment
Know the answer?
Add Answer to:
I have provided you with a sample class named FlashDrive which has been diagrammed below. Using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two ...

    Help please with a Project in C++ In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: 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(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which...

    Project 2 - Flashdrive 1.0 I have provided you with a sample class named FlashDrive which has been diagrammed below and described in the online course content. For this unit, I'd like you to identify additional member variables (data) that would be appropriate for the class FlashDrive. I would also like you to identify additional methods (functions) that would be appropriate for the class FlashDrive. Submit a .h file for FlashDrive with the new possible methods and members you have...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT