CS 52: Assignment #6 Solved

25.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

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 redefinable in this way, but many of the ones we have encounted so far are, like + and – and * and / and > 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 classtype, 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.

part_a: flashdrive 2.0

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 Sample Driver Code
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;
using namespace cs52;

void main( )
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( 1 );
drive2.pullOut( );

cs52::FlashDrive combined = drive1 + drive2;
cout << “this drive’s filled to ” << combined.getUsed( ) << endl;

cs52::FlashDrive other = combined – drive1;
cout << “the other cup’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;

int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;



  • Your submission should follow this organization:
    • part_a
      • main.cpp
      • flashdrive_2_0.h
      • flashdrive_2_0.cpp
  • 06_flashdrive_operators_ref.zip