In this assignment you will explore both inheritance and exc

In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingPool.cpp, SwimmingPoolDriver.cpp). Please provide header and implementation files for each class and a driver file ( I have posted my solution files for Project 1 at the bottom for your reference).

Create two custom exception classes inheriting from std::logic_error: OverflowingSwimmingPoolExceptionand UnderflowingSwimmingPoolException. Note that subclasses must call their parent class constructors in their initialization list.

When an illegal operation is attempted, create and throw a custom exception, rather than just printing an error message. Include try...catch blocks in your driver code to catch any exceptions. Remember to add the statement #include when working with logic_error.

See the sample driver code shown below.

############# SwimmingPool.h file #############

#ifndef SWIMMINGPOOL_H
#define SWIMMINGPOOL_H
#include
using namespace std;
class SwimmingPool {
public:
SwimmingPool( int size );
void fill( int amount );
void splash( );
void swim( );
void evaporate( int amount );
int getSize( );
int getContents( );
void setSize( int size );
friend ostream& operator << ( ostream& outs, const SwimmingPool & pool );
friend istream& operator >> ( istream& ins, SwimmingPool & pool );

SwimmingPool operator+(const SwimmingPool& other);
SwimmingPool operator-(const SwimmingPool& other);

bool operator==(const SwimmingPool& other);
bool operator!=(const SwimmingPool& other);
bool operator<(const SwimmingPool& other);
bool operator>(const SwimmingPool& other);
private:
int mySize; // how big the pool is
int myContents; // how full the pool is
};
#endif

################ SwimmingPool.cpp ##############

#include \"SwimmingPool.h\"
#include
using namespace std;
SwimmingPool::SwimmingPool( int size ) {
mySize = size;
myContents = 0;
}
void SwimmingPool::fill( int amount ) {
myContents += amount;
}
void SwimmingPool::splash( ) {
cout << \"some splashing in this pool...\" << endl;
}
void SwimmingPool::swim( ) {
cout << \"some swimming in this pool...\" << endl;
}
void SwimmingPool::evaporate( int amount ) {
myContents -= amount;
}
int SwimmingPool::getSize( ) {
return( mySize );
}
int SwimmingPool::getContents( ) {
return( myContents );
}
void SwimmingPool::setSize( int size ) {
mySize = size;
}

ostream& operator << ( ostream& outs, const SwimmingPool & pool ){
   outs<<\"Size: \"<

// Overload + operator to add two SwimmingPool objects.
SwimmingPool SwimmingPool::operator+(const SwimmingPool& other) {
   SwimmingPool spool(mySize+other.mySize);
   spool.fill(myContents+other.myContents);
  
   return spool;
}

// Overload - operator to add two SwimmingPool objects.
SwimmingPool SwimmingPool::operator-(const SwimmingPool& other) {
   int size = mySize - other.mySize;
   int contents = myContents - other.myContents;

   if(size < 0){
       cout<<\"Can not subtact smaller SwimmingPool to big SwimmingPool\"<        return *this;
   }
   if(contents < 0){
       cout<<\"Can not subtact smaller SwimmingPool to big SwimmingPool\"<        return *this;
   }

   SwimmingPool spool(size);
   spool.fill(contents);
  
   return spool;
}

bool SwimmingPool::operator==(const SwimmingPool& other) {
return mySize == other.mySize;
}

bool SwimmingPool::operator!=(const SwimmingPool& other) {
return mySize != other.mySize;
}

bool SwimmingPool::operator<(const SwimmingPool& other) {
return mySize < other.mySize;
}

bool SwimmingPool::operator>(const SwimmingPool& other) {
return mySize > other.mySize;
}

################   SwimmingPoolDriver.cpp #################

#include
using namespace std;
#include \"SwimmingPool.h\"
int main( ) {
SwimmingPool smallOne( 1 );
SwimmingPool bigOne( 1000 );
bigOne.fill( 100 );
SwimmingPool yours( 10 );
yours.fill( 1 );
SwimmingPool mine( 20 );
mine.fill( 19 );
cout << \"--some tests follow--\"<< endl;
SwimmingPool pool1 = mine + mine;
SwimmingPool pool2 = yours - yours;
if (pool1 > pool2) {
cout << \"> is working...\" << endl;
}
if (pool1 != pool2) {
cout << \"!= is working...\" << endl;
}
if (pool2 < pool1) {
cout << \"< is working...\" << endl;
}
if (pool1 == pool1) {
cout << \"== is working...\" << endl;
}
cout << \"---some broken code follows---\" << endl;;
SwimmingPool badPool = smallOne - bigOne;
//this operator above should print
//out an error message...
system(\"pause\");
return 0;
}

Solution

#ifndef SWIMMINGPOOL_H
#define SWIMMINGPOOL_H
#include<iostream>
#include<stdexcept>
using namespace std;
class OverflowingSwimmingPoolException: public logic_error
{
public:
OverflowingSwimmingPoolException ()
: std::logic_error(\"Can not subtract smaller SwimmingPool to big SwimmingPool\")
{};
};
class UnderflowingSwimmingPoolException: public logic_error
{
public:
UnderflowingSwimmingPoolException()
: std::logic_error(\"Can not subtract smaller SwimmingPool to big SwimmingPool\")
{};
};
class SwimmingPool {
public:
SwimmingPool( int size );
void fill( int amount );
void splash( );
void swim( );
void evaporate( int amount );
int getSize( );
int getContents( );
void setSize( int size );
friend ostream& operator << ( ostream& outs, const SwimmingPool & pool );
friend istream& operator >> ( istream& ins, SwimmingPool & pool );
SwimmingPool operator+(const SwimmingPool& other);
SwimmingPool operator-(const SwimmingPool& other);
bool operator==(const SwimmingPool& other);
bool operator!=(const SwimmingPool& other);
bool operator<(const SwimmingPool& other);
bool operator>(const SwimmingPool& other);
private:
int mySize; // how big the pool is
int myContents; // how full the pool is
};
#endif

#include \"SwimmingPool.h\"
#include<iostream>
#include<ostream>
using namespace std;

SwimmingPool::SwimmingPool(int size) {
mySize = size;
myContents = 0;
}

void SwimmingPool::fill(int amount) {
myContents += amount;
}

void SwimmingPool::splash() {
cout << \"some splashing in this pool...\" << endl;
}

void SwimmingPool::swim() {
cout << \"some swimming in this pool...\" << endl;
}

void SwimmingPool::evaporate(int amount) {
myContents -= amount;
}

int SwimmingPool::getSize() {
return ( mySize);
}

int SwimmingPool::getContents() {
return ( myContents);
}

void SwimmingPool::setSize(int size) {
mySize = size;
}

ostream& operator<<(ostream& outs, const SwimmingPool & pool) {

outs << \"Size: \" << pool.mySize << endl;
outs << \"Content: \" << pool.myContents << endl;
return outs;
}

// Overload + operator to add two SwimmingPool objects.

SwimmingPool SwimmingPool::operator+(const SwimmingPool & other) {
SwimmingPool spool(mySize + other.mySize);
spool.fill(myContents + other.myContents);

return spool;
}
// Overload - operator to add two SwimmingPool objects.

SwimmingPool SwimmingPool::operator-(const SwimmingPool & other) {
int size = mySize - other.mySize;
int contents = myContents - other.myContents;
try {
throw (size < 0);
} catch (OverflowingSwimmingPoolException& e) {
cout << e.what() << endl;
}
return *this;

try {
throw (contents < 0);
} catch (UnderflowingSwimmingPoolException& e) {
cout << e.what() << endl;
}
return *this;
SwimmingPool spool(size);
spool.fill(contents);

return spool;
}

bool SwimmingPool::operator==(const SwimmingPool & other) {
return mySize == other.mySize;
}

bool SwimmingPool::operator!=(const SwimmingPool & other) {
return mySize != other.mySize;
}

bool SwimmingPool::operator<(const SwimmingPool & other) {
return mySize < other.mySize;
}

bool SwimmingPool::operator>(const SwimmingPool & other) {
return mySize > other.mySize;
}

#include<iostream>
using namespace std;
#include \"SwimmingPool.h\"

int main() {
SwimmingPool smallOne(1);
SwimmingPool bigOne(1000);
bigOne.fill(100);
SwimmingPool yours(10);
yours.fill(1);
SwimmingPool mine(20);
mine.fill(19);
cout << \"--some tests follow--\" << endl;
SwimmingPool pool1 = mine + mine;
SwimmingPool pool2 = yours - yours;
if (pool1 > pool2) {
cout << \"> is working...\" << endl;
}
if (pool1 != pool2) {
cout << \"!= is working...\" << endl;
}
if (pool2 < pool1) {
cout << \"< is working...\" << endl;
}
if (pool1 == pool1) {
cout << \"== is working...\" << endl;
}
cout << \"---some broken code follows---\" << endl;
;
SwimmingPool badPool = smallOne - bigOne;
//this operator above should print
//out an error message...
system(\"pause\");
return 0;
}

In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP
In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP
In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP
In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP
In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP
In this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (SwimmingPool.h, SwimmingP

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site