Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will explore both inheritance and exception handling. Us

ID: 3787848 • Letter: I

Question

In this assignment, you will explore both inheritance and exception handling.

Use This Header:

#include <iostream>
namespace cs52 {
   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);

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

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

This Implementation Code:

#include "SwimmingPool.h"
#include <iostream>
namespace cs52 {
   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 << ", Contents: " << pool.myContents << endl;
       return outs;
   }
   istream& operator >> (istream& ins, SwimmingPool & pool) {
       cout << "Enter size: ";
       ins >> pool.mySize;
       cout << "Enter contents: ";
       ins >> pool.myContents;
       return ins;
   }

   // 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" << endl;
           return *this;
       }
       if (contents < 0) {
           cout << "Can not subtact smaller SwimmingPool to big SwimmingPool" << 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;
}

And This Driver Code:

#include <iostream>
using namespace std;
using namespace cs52;
#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;
}

Please provide header and implementation files for each class and a driver file.

Create two custom exception classes inheriting from std::logic_error: OverflowingSwimmingPoolException and 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 <stdexcept> when working with logic_error.

See the sample driver code shown below.


// Sample driver client code.

using namespace cs52;

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 << "---printing pool1---" << endl;
cout << pool1 << endl;
cout << "---printing pool2---" << endl;
cout << pool2 << endl;

cout << "---reading pool1---" << endl;
cin >> pool1;
cout << "---printing a revised pool1---" << endl;
cout << pool1 << endl;

cout << "---some broken code follows---" << endl;;
SwimmingPool badPool = smallOne - bigOne;

try {
SwimmingPool badPool = smallOne - bigOne;
cout << "This should never happen..." << endl;
}
catch( UnderflowingSwimmingPoolException uspe ) {
cout << "working..." << endl;
}
catch( OverflowingSwimmingPoolException uspe ) {
cout << "This should never happen..." << endl;
}

try {
smallOne.evaporate( 10000 );
cout << "This should never happen..." << endl;
}
catch( UnderflowingSwimmingPoolException uspe ) {
cout << "working..." << endl;
}
catch( OverflowingSwimmingPoolException uspe ) {
cout << "This should never happen..." << endl;
}

try {
smallOne.fill( 10000 );
cout << "This should never happen..." << endl;
}
catch( UnderflowingSwimmingPoolException uspe ) {
cout << "This should never happen..." << endl;
}
catch( OverflowingSwimmingPoolException uspe ) {
cout << "working..." << endl;
}

Explanation / Answer

#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

class UnderflowingSwimmingPoolException : public exception
{
virtual const char* printErr() const throw()
{
return "Swimming pool under flow.....";
}
};
class OverflowingSwimmingPoolException : public exception
{
virtual const char* printErr() const throw()
{
return "Swimming pool over flow.....";
}
};
-------------------------------------------------------------------------
#include <iostream>
#include <exception>
#include <stdexcept>


using namespace std;

class customException : public UnderflowingSwimmingPoolException,OverflowingSwimmingPoolException{
  
};

int main ()
{
customException myex;

   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 << "---printing pool1---" << endl;
   cout << pool1 << endl;
   cout << "---printing pool2---" << endl;
   cout << pool2 << endl;

   cout << "---reading pool1---" << endl;
   cin >> pool1;
   cout << "---printing a revised pool1---" << endl;
   cout << pool1 << endl;

   cout << "---some broken code follows---" << endl;;
   SwimmingPool badPool = smallOne - bigOne;
   try {
       SwimmingPool badPool = smallOne - bigOne;
       cout << "This should never happen..." << endl;
   }
   catch( UnderflowingSwimmingPoolException uspe ) {
   cout << uspe,printErr() << endl;
   }
   catch( OverflowingSwimmingPoolException uspe ) {
   cout << uspe,printErr() << endl;
   }

   try {
       smallOne.evaporate( 10000 );
       cout << "This should never happen..." << endl;
   }
   catch( UnderflowingSwimmingPoolException uspe ) {
       cout << uspe,printErr() << endl;
   }
   catch( OverflowingSwimmingPoolException uspe ) {
       cout << uspe,printErr() << endl;
   }

   try {
   smallOne.fill( 10000 );
   cout << "This should never happen..." << endl;
   }
   catch( UnderflowingSwimmingPoolException uspe ) {
   cout << uspe,printErr() << endl;
   }
   catch( OverflowingSwimmingPoolException uspe ) {
       cout << uspe,printErr() << endl;
   }
return 0;
}