1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <iostream>
#include "FlashDrive.h"// header file for the class
#include <stdexcept>
using namespace std;
using namespace cs52;
void main( )
{
int cap1=0,used1=0,cap2=0,used2=0;
cout << "Welcome to Howie's Flash USB Drive Store!\n" //added this from the previous flashdrive provided
<< endl;
//Getting input from user
cout<< "For drive 1 please enter total capacity(>0,and number only),\nand storage used(number only): ";
cin>> cap1 >> used1;
cout<<"\n"<<cap1 << " "<< used1<<"\n";
cout<< "\nFor drive 2 please enter total capacity(>0,and number only),\nand storage used(number only): ";
cin>> cap2 >> used2;
cout<<"\n"<<cap2 <<" "<< used2<<"\n";
//Definition and initialization of drive1 and drive2
FlashDrive drive1( cap1, used1, false );//get the parameters for capacity,initial used data, and initial pluggedin check
FlashDrive drive2( cap2, used2, false );
//FlashDrive empty(0, 0, false);
cout<<"\nCapacity(1-2): "<< drive1.getCapacity()<<" "<<drive2.getCapacity() <<" Used Storage(1-2): "<< drive1.getUsed()<<" "<< drive2.getUsed()<<endl;
//Calling the functions for drive1 and drive2
//write data is added to the my_StorageUsed
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData(used1);//user input as param
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData(used2);//user input as param
drive2.pullOut( );
try {
//Defined combined and used it to hold drive1 and drive2 using overloaded "+" and "=" operators
FlashDrive combined = drive1 + drive2 ;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
//Defined other and used it to hold (drive1+drive2)-drive1 by using overloaded "-" and "=" functions to get
FlashDrive other = combined - drive1;
cout << "the other cup's filled to " << other.getUsed( ) << endl;
//checking for the conditions using overloaded bool ">" and "<" argument
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;
}
} catch (logic_error) {
cout<<"caught logic_error"<<endl;
cout<<"Cannot check the conditions"<<endl;
}
// let's throw some exceptions...
try {
FlashDrive 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 ) {
cout << "\nNot enough capacity for this..\n";
}
try {
cs52::FlashDrive f( -1, -1, false );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
cout<<"\nNegative input\n";
}
// work with the new stuff added for Unit 16!!!
cs52::FlashDrive * drive3=NULL;
// careful...
cout << drive3 << endl; //Prints "NULL Pointer" warning
drive3 = &drive2; //puts drive2 capacity and used into drive3
cout <<"\n"<< drive3 <<"^^^ drive3=&drive2 "<< endl;
drive3 = new FlashDrive();
cout <<"\nInput for drive3 capacity and used: ";
cin >> drive3; //user input for drive3 capacity and used
cout <<"\n"<< drive3 <<"^^^ drive3 after input "<< endl;
delete( drive3 );
}
|