CS 52: Assignment #12 Solved

25.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

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

Securely Powered by: Secure Checkout

Description

5/5 - (2 votes)

Directions:
Please complete the following assignment to signify your completion of Unit 17.  All programming projects need to be completed and submitted electronically, following the Electronic Submission Guidelines discussed in class.  Please cherry-pick from your solution folder and submit just the .cpp and .h  files you created or modified as well as .exe file that Visual Studio built from your code.

Background:
This assignment deals with inheritance.  Inheritance is one of the major principles of object-oriented programming.  In C++, one of the biggest goals is “code reuse”.  Inheritance accomplishes this.  In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct.  Constructor implementations must use an initialization list.  Please review the book and the online content on these issues so you know how it works before you begin..

part_a: flashdrive 4.0 (inheritance)

The purpose of this assignment is to work with exceptions and inheritance.  As you may recall, I have provided you with a sample class named FlashDrive which has been diagrammed below.  You will reuse code from previous assignments and further extend it.  I’d like you to enhance this class so that invoking its methods or operators potentially throw a custom exception, rather than just a std::logic_error.  As you may recall, you can create a logic_error by passing a string value to its constructor.  Officially, you should also say #include <stdexcept> to begin working with logic_error, but Visual Studio (being a badly behaved child…) let’s you get away with it.

For this assignment, you will want to focus on subclassing std::logic_error.  In each exceptional situation, rather than writing errors to cout or throwing std::logic_error, please create and throw a custom subclass as shown below:

Please remember that subclasses really MUST call their parent class constructors by using an initialization list.

FlashDrive

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;

Sample Driver Code

FlashDrive empty;
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );FlashDrive * nullPointer = NULL;
FlashDrive * pointer = new FlashDrive( 10, 0, false );

drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
cout << drive1 << endl;

drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
cout << drive2 << endl;

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

FlashDrive other = combined – drive1;
cout << “the other cup’s filled to ” << other.getUsed( )
<< endl;
cout << other << 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( UnderflowingFlashDriveException ) {
// 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( OverflowingFlashDriveException ) {
// an exception should get thrown…
// so the lines of code here should
// be run, not the cout statement…
}

try {
FlashDrive f( -1, -1, false );
cout << “something not right here…” << endl;
} catch( UnderflowingFlashDriveException ) {
// an exception should get thrown…
// so the lines of code here should
// be run, not the cout statement…
}

// let’s work with pointers…

cout << nullPointer << endl; // be careful…
cout << pointer << endl;
cin >> pointer;
cout << pointer; // did the values change?
cin >> drive1;
cout << drive1 << endl;

 

 

part_b: Gambler

I’d like you to make two inherited gambler classes as described below.  A Gambler is a casino player.  A SlotPlayer is a kind of Gambler who plays casino slot machines.  The relationship between these two classes is shown in the diagram shown below.

Please remember that subclasses really MUST call their parent class constructors by using an initialization list.

Gambler

Driver Code

Gambler( std::string name );

virtual void win( );
virtual void lose( );

Gambler g( “Tom” );
SlotPlayer sp( “Tom’s Mother” );g.win( );
sp.lose( );
g.lose( );
sp.win( );
std::string myName; // protected

SlotPlayer

Sample Output

SlotPlayer( std::string name );

virtual void win( );
virtual void lose( );

The Great Gambler Tom wins!
The SlotPlayer Tom’s Mother loses  🙁
The Great Gambler Tom loses!
The SlotPlayer Tom’s Mother wins  🙂

 

  • Your submission should follow this organization, all of which have been provided:
    • part_a
      • main.cpp
      • flashdrive_4_0.h
      • flashdrive_4_0.cpp
    • part_b
      • Gambler.cpp
      • Gambler.h
      • SlotPlayer.cpp
      • SlotPlayer.h
      • main.cpp
  • 11_flashdrive_inheritance.zip