//Swimming Pool Header// #include using namespace std; #include \"SwimmingPool.h
ID: 3789088 • Letter: #
Question
//Swimming Pool Header//
#include
using namespace std;
#include "SwimmingPool.h"
using namespace cs52;
#ifndef SWIMMINGPOOL_H
#define SWIMMINGPOOL_H
#include
using namespace std;
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 );
SwimmingPool operator+ (SwimmingPool &);
SwimmingPool operator- (SwimmingPool &);
bool operator> (SwimmingPool &);
bool operator< (SwimmingPool &);
bool operator!= (SwimmingPool &);
bool operator== (SwimmingPool &);
friend ostream& operator << ( ostream& outs, const SwimmingPool & pool );
friend istream& operator >> ( istream& ins, SwimmingPool & pool );
private:
int mySize; // how big the pool is
int myContents; // how full the pool is
};
}
#endif
**************************************************************
// Swimming pool //
#include "SwimmingPool.h"
#include
using namespace std;
namespace cs52{
SwimmingPool::SwimmingPool( int size ) {
mySize = size;
myContents = 0;
}
void SwimmingPool::fill( int amount ) {
if (amount< 0)
{ cout<<" The contents cannot be negative"<
system("pause");
}
if (myContents + amount > mySize)
{ cout<<" New content value cannot be greater than size"<
myContents= mySize;
}
else
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 ) {
if(amount< 0)
{ cout<<" The contents cannot be negative"<< endl;
system("pause");
}
if(myContents - amount< 0)
{ cout<<"New contents cannot be negative"<
myContents=0;
}
else
myContents -= amount;
}
int SwimmingPool::getSize( ) {
return( mySize );
}
int SwimmingPool::getContents( ) {
return( myContents );
}
void SwimmingPool::setSize( int size ) {
if(size<0)
{ cout<<" The size cannot be negative"<< endl;
system("pause");
}
mySize = size;
}
ostream& operator<<( std::ostream& outs, const SwimmingPool & pool )
{
outs<<"Pool!!"<< pool.myContents<<" "<
return outs;
}
istream& operator>>( std::istream& ins, SwimmingPool & pool )
{
cout<<"Enter size of pool:";
ins>>pool.mySize;
cout<<"Enter contents:";
ins>>pool.myContents;
return ins;
}
SwimmingPool SwimmingPool::operator+(SwimmingPool &pool)
{
int poolContents=pool.getContents();
int poolSize=pool.getSize();
int newSize=poolSize+ this->mySize;
SwimmingPool pool2(newSize);
int newContents= poolContents+this->myContents;
if( newContents > newSize)
{
cout<<"New Contents are greater than the contents"<
return 0;
}
pool2.fill(newContents);
return pool2;
}
SwimmingPool SwimmingPool::operator-(SwimmingPool &pool)
{
int poolContents=pool.getContents();
int poolSize=pool.getSize();
int newSize=this->mySize-poolSize;
SwimmingPool pool2(newSize);
int newContents= this->myContents - poolContents;
if( newContents <= 0)
{
cout<<" New Contents are less than or equal to Zero"<
return 0;
}
pool2.fill(newContents);
return pool2;
}
bool SwimmingPool::operator>(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize > poolSize);
}
bool SwimmingPool::operator<(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize < poolSize);
}
bool SwimmingPool::operator!=(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize != poolSize);
}
bool SwimmingPool::operator==(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize == poolSize);
}
}
**************************************************************************
// Swimming pool driver code//
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");
}
Project 2: Inheritance n this assignment, you will explore both inheritance and exception handling. Start with your completed solution files from Project 1 (Swimming Pool.h, SwimmingPool.cpp, SwimmingPoolDriver.cpp). Please provide header and implementation files for each class and a driver file. Create two custom exception classes inheriting from std:logic error: Overflowin wimming PoolException 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 #includeExplanation / Answer
#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
//logic_error Class
class logic_error : public exception
{
public:
logic_error(string msg)
{
message = msg;
}
string what()
{
return message;
}
~logic_error() throw() {}
private:
string message;
};
//OverflowingSwimmingPoolException class
class OverflowingSwimmingPoolException : logic_error
{
public:
OverflowingSwimmingPoolException(string error ) : logic_error(error)
{
}
};
//UnderflowingSwimmingPoolException class
class UnderflowingSwimmingPoolException : logic_error
{
public:
UnderflowingSwimmingPoolException(string error ) : logic_error(error)
{
}
};
// 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 );
SwimmingPool operator+ (SwimmingPool &);
SwimmingPool operator- (SwimmingPool &);
bool operator> (SwimmingPool &);
bool operator< (SwimmingPool &);
bool operator!= (SwimmingPool &);
bool operator== (SwimmingPool &);
friend ostream& operator << ( ostream& outs, const SwimmingPool & pool );
friend istream& operator >> ( istream& ins, SwimmingPool & pool );
private:
int mySize; // how big the pool is
int myContents; // how full the pool is
};
//SwimmingPool class ends here
// Implementation of all methods of SwimmingPool class
SwimmingPool::SwimmingPool( int size ) {
mySize = size;
myContents = 0;
}
void SwimmingPool::fill( int amount ) {
if (amount< 0)
{
throw UnderflowingSwimmingPoolException("Underflow");
//std::cout<<" The contents cannot be negative"<<std::endl;
//std::system("pause");
}
if (myContents + amount > mySize)
{
throw OverflowingSwimmingPoolException("Overflow");
//std::cout<<" New content value cannot be greater than size"<<std::endl;
//myContents= mySize;
}
else
myContents+=amount;
}
void SwimmingPool::splash( ) {
std::cout << "some splashing in this pool..." << std::endl;
}
void SwimmingPool::swim( ) {
std::cout << "some swimming in this pool..." << std::endl;
}
void SwimmingPool::evaporate( int amount )
{
if(amount< 0)
{
throw UnderflowingSwimmingPoolException("Underflow");
//std::cout<<" The contents cannot be negative"<< std::endl;
//std::system("pause");
}
if(myContents - amount< 0)
{
throw UnderflowingSwimmingPoolException("Underflow");
//std::cout<<"New contents cannot be negative "<< std::endl;
//myContents=0;
}
else
myContents -= amount;
}
int SwimmingPool::getSize( ) {
return( mySize );
}
int SwimmingPool::getContents( ) {
return( myContents );
}
void SwimmingPool::setSize( int size ) {
if(size<0)
{ std::cout<<" The size cannot be negative"<< std::endl;
std::system("pause");
}
mySize = size;
}
ostream& operator<<( std::ostream& outs, const SwimmingPool & pool )
{
outs<<"Pool!!"<< pool.myContents<<" "<< std::endl;
return outs;
}
istream& operator>>( std::istream& ins, SwimmingPool & pool )
{
std::cout<<"Enter size of pool:";
ins>>pool.mySize;
std::cout<<"Enter contents:";
ins>>pool.myContents;
return ins;
}
SwimmingPool SwimmingPool::operator+(SwimmingPool &pool)
{
int poolContents=pool.getContents();
int poolSize=pool.getSize();
int newSize=poolSize+ this->mySize;
SwimmingPool pool2(newSize);
int newContents= poolContents+this->myContents;
if( newContents > newSize)
{
throw OverflowingSwimmingPoolException("Overflow");
//std::cout<<"New Contents are greater than the contents "<<std::endl;
//return 0;
}
pool2.fill(newContents);
return pool2;
}
SwimmingPool SwimmingPool::operator-(SwimmingPool &pool)
{
int poolContents=pool.getContents();
int poolSize=pool.getSize();
int newSize=this->mySize-poolSize;
SwimmingPool pool2(newSize);
int newContents= this->myContents - poolContents;
if( newContents < 0)
{
throw UnderflowingSwimmingPoolException("underflow");
//std::cout<<" New Contents are less than or equal to Zero "<< std::endl;
//return 0;
}
pool2.fill(newContents);
return pool2;
}
bool SwimmingPool::operator>(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize > poolSize);
}
bool SwimmingPool::operator<(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize < poolSize);
}
bool SwimmingPool::operator!=(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize != poolSize);
}
bool SwimmingPool::operator==(SwimmingPool &pool)
{
int poolSize=pool.getSize();
return (this->mySize == poolSize);
}
// Implementation of all methods of SwimmingPool class ends here
// Swimming pool driver code//
int main()
{
SwimmingPool smallOne(1);
SwimmingPool bigOne(1000);
bigOne.fill(100);
SwimmingPool yours(10);
yours.fill(1);
SwimmingPool mine(20);
mine.fill(19);
std::cout << "--some tests follow--"<< std::endl;
SwimmingPool pool1 = mine + mine;
SwimmingPool pool2 = yours - yours;
if (pool1 > pool2) {
std::cout << "> is working..." << std::endl;
}
if (pool1 != pool2) {
std::cout << "!= is working..." << std::endl;
}
if (pool2 < pool1) {
std::cout << "< is working..." << std::endl;
}
if (pool1 == pool1) {
std::cout << "== is working..." << std::endl;
}
std::cout << "---some broken code follows---" << std::endl;
//handling of exception
try{
SwimmingPool badPool = smallOne - bigOne;
cout << "This should never happen..."<<endl;
}
catch(UnderflowingSwimmingPoolException uspe){
cout <<"working..." << endl;
}
catch(OverflowingSwimmingPoolException uspe){
cout << "This shuld never happen..."<<endl;
}
try{
smallOne.evaporate(10000);
cout << "This shuld never happen..."<<endl;
}
catch(UnderflowingSwimmingPoolException uspe){
cout <<"working..." << endl;
}
catch(OverflowingSwimmingPoolException uspe){
cout << "This shuld never happen..."<<endl;
}
try{
smallOne.fill(10000);
cout << "This shuld never happen..."<<endl;
}
catch(UnderflowingSwimmingPoolException uspe){
cout << "This shuld never happen..."<<endl;
}
catch(OverflowingSwimmingPoolException uspe){
cout <<"working..." << endl;
}
system("pause");
}
// I wrritten all files together please split them as per your need . I have provided the class name just before each class start.
Below is the output I got:
--some tests follow--
> is working...
!= is working...
< is working...
== is working...
---some broken code follows---
working...
working...
working...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.