As a part of our exam review package for next week, we are asked to understand pointers in overloaded operators-- something I am finding it very difficult to get and the instructor is refusing to give us any example, and asking his for independent study.
Apparently we will be having one of these problems on the final and I want to be sure I properly understand this practice exercise.
This practice concerns the issue of operator overloading. And we are asked to implement << and >>. And we are to :
Using the FlashDrive class you have been working with, 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
NOTE: These ideas will be a part on next weeks assignment!
I am having a lot of trouble getting
friend std::ostream& operator <<( std::ostream& outs, const FlashDrive * drive );
#include <iostream>
#include "FlashDrive.h"
usingnamespace cs52;
void main( )
{
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( 1 );
drive2.pullOut( );
// read in a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be read in
cs52::FlashDrive sample;
cin >> sample;
// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
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;
}
// let's throw some exceptions...
try {
empty = empty – combined;
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;
drive3 = &drive2;
cout << drive3 << endl;
drive3 = new FlashDrive();
cin >> drive3;
cout << drive3 << endl;
delete( drive3 );
}
the '>>' in cin >> sample
and the '<<' in cout << sample
as expressed bewlow
1 2 3 4 5 6 7
cs52::FlashDrive sample;
cin >> sample;
// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
I know they sound self explanatory, but I can't seem to fix them with out causing errors on other parts of the code
------ Build started: Project: Pointer FlashDrive, Configuration: Debug Win32 ------
Build started 8/5/2013 3:18:50 AM.
InitializeBuildStatus:
Touching "Debug\Pointer FlashDrive.unsuccessfulbuild".
ClCompile:
FlashDriver.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer
flashdrive\flashdriver.cpp(107): fatal error C1075: end of file found before the left
brace '{' at '\\psf\home\documents\visual studio 2010\projects\pointer
flashdrive\pointer flashdrive\flashdrive.h(4)' was matched
FlashDrive.cpp
\\psf\home\documents\visual studio 2010\projects\pointer flashdrive\pointer
flashdrive\flashdrive.cpp(63): fatal error C1075: end of file found before the left
brace '{' at '\\psf\home\documents\visual studio 2010\projects\pointer
flashdrive\pointer flashdrive\flashdrive.h(4)' was matched
Generating Code...
Build FAILED.
Time Elapsed 00:00:01.82
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
and doing what I said above fixed all but these two errors
#include <iostream>
#include <cstdlib>
#include "FlashDrive.h"
usingnamespace std;
void main( )
{
usingnamespace cs52;
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( 1 );
drive2.pullOut( );
// read in a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be read in
cs52::FlashDrive *sample;
cin >> sample;
// print out a FlashDrive...
// the class designer for FlashDrive (that's you!)
// gets to decide which fields matter and should be printed
cout << sample << endl;
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;
}
// let's throw some exceptions...
try {
empty = empty - combined;
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
drive2.writeData( 10000 );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown...
// so the lines of code here should
// be run, not the cout statement...
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3 = NULL;
// careful...
cout << drive3 << endl;
drive3 = &drive2;
cout << drive3 << endl;
drive3 = new FlashDrive();
cin >> drive3;
cout << drive3 << endl;
delete( drive3 );
}
Btw: you need the ' ' only to separate two variables. At the end it makes no sense. The reason is that during reading the stream can detect were one variable ends and the other begins.