Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Background: In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator

Background: 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 + , - , * , / , > and < for example. It is a class' choice to do this, so mostly it is viewed as a driver code convenience to support them. But because so many of us have an assumption that + should do addition but also perform string concatenation when working with textual data. Each operator becomes a friend function in C++ that your class can implement. The arguments to most of the operator overloads are defined as const FlashDrive &. This syntax is a new kind of parameter passing called const-reference parameters. For a class type, like FlashDrive, const & signifies a read-only argument that cannot be changed by the function that receives this object. The compiler enforces this restriction and, for classes, const & simulates a pass-by-value mechanism without the overhead of copying the object, which might take a tremendous amount of time away from our program. Read the book and demo source examples carefully to see how this is done. Trust me, the first time you work with operators in C++, it is a very error-prone process. My best advice to you is to complete one operator at a time. Flashdrive 2.0 Rules of the flashdrive comparisons:

1.The addition operator (+) combines two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and add it to a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB). Then, the operator should mark that the new flash drive has used the sum of the two operand's flashdrives (for instance, if one of the operands used 5GB and the other used 12GB, the resulting flashdrive has used 17GB).

2.The subtraction operator (-) substracts two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and add it to a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB). Then, the operator should remove the right operand's storage from the left operand (for instance, if the left operand has used 20GB and the other used 12GB, the resulting flashdrive has used 8GB).

3.The less than operator (<) should compare the if the left operand's storage used is less than the right operand's storage used.

4.The greater than operator (>) should compare the if the left operand's storage used is greater than the right operand's storage used.

Using flashdrive_2_0.cpp, 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. 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. Finally, I would also like you to place FlashDrive in namespace cs52 which will affect the resulting driver code as well as the class' .h and .cpp files. FlashDrive( ); FlashDrive( int capacity, int used, bool pluggedIn ); void plugIn( ); bool isPluggedIn( ); void pullOut( ); void writeData( int amount ); void eraseData( int amount ); void formatDrive( ); void setCapacity( int amount ); void setUsed( int amount ); int getCapacity( ); int getUsed( );Background: 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 + , - , * , / , > and < for example. It is a class' choice to do this, so mostly it is viewed as a driver code convenience to support them. But because so many of us have an assumption that + should do addition but also perform string concatenation when working with textual data. Each operator becomes a friend function in C++ that your class can implement. The arguments to most of the operator overloads are defined as const FlashDrive &. This syntax is a new kind of parameter passing called const-reference parameters. For a class type, like FlashDrive, const & signifies a read-only argument that cannot be changed by the function that receives this object. The compiler enforces this restriction and, for classes, const & simulates a pass-by-value mechanism without the overhead of copying the object, which might take a tremendous amount of time away from our program. Read the book and demo source examples carefully to see how this is done. Trust me, the first time you work with operators in C++, it is a very error-prone process. My best advice to you is to complete one operator at a time. Flashdrive 2.0 Rules of the flashdrive comparisons:

The addition operator (+) combines two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and add it to a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB). Then, the operator should mark that the new flash drive has used the sum of the two operand's flashdrives (for instance, if one of the operands used 5GB and the other used 12GB, the resulting flashdrive has used 17GB). The subtraction operator (-) substracts two flashdrives by created a new flashdrive that has the larger capacity of the two operand flashdrives (e.g. if you have a 20GB flash drive and add it to a 10GB flashdrive, the resulting flashdrive has 20GB of space, not 10GB). Then, the operator should remove the right operand's storage from the left operand (for instance, if the left operand has used 20GB and the other used 12GB, the resulting flashdrive has used 8GB). The less than operator (<) should compare the if the left operand's storage used is less than the right operand's storage used. The greater than operator (>) should compare the if the left operand's storage used is greater than the right operand's storage used.

Using flashdrive_2_0.cpp, 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. 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. Finally, I would also like you to place FlashDrive in namespace cs52 which will affect the resulting driver code as well as the class' .h and .cpp files. FlashDrive( ); FlashDrive( int capacity, int used, bool pluggedIn ); void plugIn( ); bool isPluggedIn( ); void pullOut( ); void writeData( int amount ); void eraseData( int amount ); void formatDrive( ); void setCapacity( int amount ); void setUsed( int amount ); int getCapacity( ); int getUsed( );

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How to solve maths problems with examples

Answered: 1 week ago

Question

Explain Coulomb's law with an example

Answered: 1 week ago

Question

What is operating system?

Answered: 1 week ago

Question

2. KMPG LLP

Answered: 1 week ago

Question

5. Wyeth Pharmaceuticals

Answered: 1 week ago