Background: This assignment revisits the issue of operator overloading. In this
ID: 3667321 • Letter: B
Question
Background:
This assignment revisits the issue of operator overloading. In this assignment, you are asked to implement << and >>. These operators are fairly tricky because their parameters are unusual and the way you need to code them is not exactly straightforward. Please read over this issue in the book and review the online content so it is clear to you before you begin. In addition, this assignment starts dealing with pointers to classes. Students find pointers very challenging because code that uses pointer doesn't always work. It depends on whether the pointer is any good, pointing to a non-NULL value. If you choose to continue in the C++ track at SMC, you'll need to be very comfortable with pointers.
Project 12: Pointer FlashDrive
Using the FlashDrive class provided earlier ( VS2012 or XCode7), upgrade the class so that operator << and operator >> work well with pointers (that is, FlashDrive *). Youll need to re-overload these operators, adding the function:
friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );
friend std::istream& operator >>( std::istream& ins, FlashDrive * & drive );
HINT: Be very careful to test for NULL...
FlashDrive Class
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( );
Driver Code
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 * ptrDrive = new FlashDrive( 0, 0, false );
FlashDrive * nullDrive = NULL; // a pointer to anything can be NULL...
// let's work with the pointers...
cin >> ptrDrive;
cin >> nullDrive; // be careful...
cout << ptrDrive << endl;
cout << nullDrive << endl; // be careful...
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;
// Let's try to get some exceptions...
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
FlashDrive Class
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( );
int my_StorageCapacity;int my_StorageUsed;
bool my_IsPluggedIn;
Explanation / Answer
main.cpp
#include <iostream>
#include "FlashDrive.h"
using namespace std;
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;
}
FlashDrive.cpp
#include <iostream>
#include "FlashDrive.h"
using namespace std;
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 );
}
void FlashDrive::setCapacity( int amount ) {
my_StorageCapacity = amount;
}
int FlashDrive::getUsed( ) {
return( my_StorageUsed );
}
void FlashDrive::setUsed( int amount ) {
my_StorageUsed = amount;
}
bool FlashDrive::isPluggedIn( ) {
return( my_IsPluggedIn );
}
/**
* @Overload '+' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (add-up values of their capacities; used capacities, sets
* it to the unplugged condition).
*/
FlashDrive operator + (const FlashDrive& drive1, const FlashDrive& drive2) {
// create a new FlashDrive
FlashDrive newFlashDrive =
FlashDrive(drive1.my_StorageCapacity + drive2.my_StorageCapacity,
drive1.my_StorageUsed + drive2.my_StorageUsed, false);
// defensive programming
// print out an error message if capacity or storage value is negative.
if (newFlashDrive.getCapacity() < 0 || newFlashDrive.getUsed() < 0) {
cout << "Error: the created FlashDrive has a negative";
cout << "capacity or storage value!" << endl;
// print out an error message if capacity is less than the storage value.
} else if (newFlashDrive.getCapacity() < newFlashDrive.getUsed()) {
cout << "Error: a value stored on the created FlashDrive";
cout << "exceeds its capacity!" << endl;
}
// return the created FlashDrive
return newFlashDrive;
}
/**
* @Overload '-' operator.
* Creates and returns a new FlashDrive from the two arguments by combining
* their contents (subtract values of their capacities; used capacities, sets
* it to the unplugged condition).
*/
FlashDrive operator - (const FlashDrive& drive1, const FlashDrive& drive2) {
// create a new FlashDrive
FlashDrive newFlashDrive =
FlashDrive(drive1.my_StorageCapacity - drive2.my_StorageCapacity,
drive1.my_StorageUsed - drive2.my_StorageUsed, false);
// defensive programming
// print out an error message if capacity or storage value is negative.
if (newFlashDrive.getCapacity() < 0 || newFlashDrive.getUsed() < 0) {
cout << "Error: the created FlashDrive has a negative";
cout << "capacity or storage value!" << endl;
// print out an error message if capacity is less than the storage value.
} else if (newFlashDrive.getCapacity() < newFlashDrive.getUsed()) {
cout << "Error: a value stored on the created FlashDrive";
cout << "exceeds its capacity!" << endl;
}
// return the created FlashDrive
return newFlashDrive;
}
/**
* @Overload '<' operator.
* Returns true if the amount of free space on drive1 is less than on drive2,
* false otherwise.
*/
bool operator < (const FlashDrive& drive1, const FlashDrive& drive2) {
return (drive1.my_StorageCapacity - drive1.my_StorageUsed <
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity < drive2.my_StorageCapacity);
}
/**
* @Overload '>' operator.
* Returns true if the amount of free space on drive1 is bigger than on drive2,
* false otherwise.
*/
bool operator > (const FlashDrive& drive1, const FlashDrive& drive2) {
return (drive1.my_StorageCapacity - drive1.my_StorageUsed >
drive2.my_StorageCapacity - drive2.my_StorageUsed);
/* The second option is to compare the flash drives by their total capacity
* In order to use it comment above return statement and uncomment return
* statement below
*/
//return(drive1.my_StorageCapacity > drive2.my_StorageCapacity);
}
/**
* @Overload '<<' operator.
* Prints out values of the private member variables of the FlashDrive class.
* Uses iostream:
*/
ostream & operator << (ostream& outs, const FlashDrive& drive) {
outs << "Capacity: " << drive.my_StorageCapacity << " kilobytes; " << endl;
outs << "Used space: " << drive.my_StorageUsed << " kilobytes; " << endl;
outs << "Free space: " << (drive.my_StorageCapacity - drive.my_StorageUsed)
<< " kilobytes; " << endl;
outs << "The flash drive is "
<< (drive.my_IsPluggedIn == true ? "plugged-in. " : "unplugged. ");
return outs;
}
/**
* @Overload '>>' operator.
* Reads-in and sets the values of the private member variables of
* the FlashDrive class.
* Uses iostream:
*/
istream & operator >> (istream& ins, FlashDrive& drive) {
// Prompt the user to enter the capacity of the drive.
int capacity;
do {
cout << "Enter the capacity of the flash drive in kilobytes ";
cout << "(positive integer): ";
ins >> capacity;
} while (capacity <= 0);
// Prompt the user to enter the used capacity of the drive.
int usedCapacity;
do {
cout << "Enter the used capacity of the flash drive in kilobytes ";
cout << "(non-negative integer): ";
ins >> usedCapacity;
} while (usedCapacity < 0 || usedCapacity > capacity);
// Prompt the user to enter the state of the drive.
bool isPluggedIn;
do{
cout << "Is it plugged-in [1=yes/0=no]?: ";
ins >> isPluggedIn;
if (isPluggedIn == 0 || isPluggedIn == 1) {
break;
}
ins.clear();
} while (true);
// Assign the entered values to the private variables of FlashDrive.
drive.my_StorageCapacity = capacity;
drive.my_StorageUsed = usedCapacity;
drive.my_IsPluggedIn = isPluggedIn;
return ins;
}
FlashDrive.h
#ifndef FLASHDRIVE_H
#define FLASHDRIVE_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( );
void setCapacity( int amount );
int getUsed( );
void setUsed( int amount );
bool isPluggedIn( );
friend FlashDrive operator + ( const FlashDrive & drive1, const FlashDrive & drive2 );
friend FlashDrive operator - (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator < (const FlashDrive & drive1, const FlashDrive & drive2);
friend bool operator > (const FlashDrive & drive1, const FlashDrive & drive2);
friend ostream & operator << (ostream & outs, const FlashDrive & drive);
friend istream & operator >> (istream & ins, FlashDrive & drive);
private:
int my_StorageCapacity; // in kilobytes
int my_StorageUsed; // in kilobytes
bool my_IsPluggedIn; // am I attached to a computer?
};
#endif
Sample Output
this drive's filled to 6
the other drives's filled to 1
looks like combined is bigger...
looks like other is bigger...
looks like drive1 is smaller...
Here is drive1....
Capacity: 10 kilobytes;
Used space: 5 kilobytes;
Free space: 5 kilobytes;
Free space: 5 kilobytes;
The flash drive is unplugged.
Let's set drive1....
Enter the capacity of the flash drive in kilobytes
(positive integer): 10
Enter the used capacity of the flash drive in kilobytes
(non-negative integer): 5
Is it plugged-in [1=yes/0=no]?: 1
Here is drive1. Notice the changes...
Capacity: 10 kilobytes;
Used space: 5 kilobytes;
Free space: 5 kilobytes;
The flash drive is plugged-in.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.