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

The purpose of this assignment is to work with exceptions and inheritance. Enhan

ID: 666683 • Letter: T

Question


The purpose of this assignment is to work with exceptions and inheritance. Enhance the Mailbox class so that the operators and other functions of the class throw subclassed exceptions when things go wrong, rather than just print out an error message. For example, if you wind up with a Mailbox with a size value that exceeds its contents, throw a OverflowingMailboxException, rather than just a "normal" std::logic_error. If you wind up with a negative size or contents value, throw a UnderflowingMailboxException, rather than just a "normal" std::logic_error.

Please be sure that your subclasses call their parent class constructors.

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. You say #include <stdexcept> to begin working with logic_error.

HINT: Recall that you can create a logic_error by passing a string message. For example,

  std::logic_error error( "Bad News" );

While not required with Visual Studio, please #include <stdexcept> when working with this class. Linux fans will require this include; its optional for Windows users but wont hurt anything if you do it.

Mailbox

void setSize( int amount );
int getSize( );
void setAddress( string address );
string getAddress();
int getContents();

void pickupMail();
int deliverMail( int pieces );

Driver Code

Mailbox yours( "your address" );
Mailbox mine( "1900 Pico Boulevard" );
Mailbox test( "test" );

yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );

yours.deliverMail( 5 );
mine.deliverMail( 6 );
test.deliverMail( 5 );


cout << test << endl;

try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( UnderflowingMailboxException ume ) {
cout << "caught underflowing exception" << endl;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}


/// test should be unchanged
cout << test << endl;

try {
// how can you deliver negative pieces of mail??
// an exception should get thrown
test.deliverMail( -100 );

} catch( UnderflowingMailboxException ume ) {
cout << "caught underflowing exception" << endl;
} catch( std::logic_error le ) {
cout << "deliverMail failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have 5 pieces of mail in a box that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );

} catch( OverflowingMailboxException ume ) {
cout << "caught overflowing exception" << endl;
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}

/// test should be unchanged
cout << test << endl;

try {
// how can you have a negative size??
test.setSize( -33 );
} catch( UnderflowingMailboxException ume ) {
cout << "caught underflowing exception" << endl;
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}

try {
// how can you have 100 pieces of mail in a box that
// can only hold 5??
// an exception should get thrown
test.deliverMail( 100 );

} catch( OverflowingMailboxException ume ) {
cout << "caught overflowing exception" << endl;
} catch( std::logic_error le ) {
cout << "deliverMail failed" << endl;
}

Mailbox

Mailbox();
Mailbox( string address );

void setSize( int amount );
int getSize( );
void setAddress( string address );
string getAddress();
int getContents();

void pickupMail();
int deliverMail( int pieces );

string my_Address;
int my_Size;
int my_Contents;

Explanation / Answer

main.cpp

int main()
{
    Mailbox yours("Your address");
    Mailbox mine("1900 Pico Boulvard");
    Mailbox test("test");
    yours.setSize(10);
    mine.setSize(10);
    test.setSize(5);
    yours.deliverMail(5);
    mine.deliverMail(6);
    test.deliverMail(5);
    try{
      //contents will become negative..
      //an exception should get thrown
      test=yours-mine;
    }catch(UnderflowingMailboxException umex){
      cout<<"An exception is caught"<<endl;
    }catch(std::logic_error le) {
      cout<<"subtraction failed"<<endl;
    }
    try{
      //how can we have a negative size?
      test.setSize( -33 );
    }catch(UnderflowingMailboxException umex) {
      cout<<"An exception is caught"<< endl;
    }catch(std::logic_error le) {
      cout<<"setSize failed"<<endl;
    }
    return 0;
}

mailbox.h

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

class Mailbox {
public:
    Mailbox(); //new mailbox
    Mailbox(string address); //new mailbox, if we know its address
    void setAddress(string address);
    string getAddress();      
    int getSize();
    void setSize(int size);
    int getContents();
    int deliverMail(int pieces);
    void pickupMail();
private:
    int my_Size;
    int my_Contents;
    string my_Address;
};


//exception subclasses

class UnderflowingMailboxException :public logic_error
{
public:
    UnderflowingMailboxException(string const& my_string);
};

class OverflowingMailboxException :public logic_error
{
public:
    OverflowingMailboxException(string const& my_string);
};

mail.cpp

#include "mailbox.h"

using namespace std;

Mailbox::Mailbox() {
    my_Size=0;
    my_Contents=0;
    my_Address="?address?";
}

Mailbox::Mailbox(string address){
    my_Size=0;
    my_Contents=0;
    my_Address=address;
}

void Mailbox:: setAddress(string addres){
    my_Address=addres;
}

string Mailbox::getAddress(){
    return(my_Address);
}

int Mailbox::getSize(){
    return( my_Size );
}

void   Mailbox::setSize(int size) //improved setSize() handles the exceptions
{
    if (my_Size<my_Contents){
        throw logic_error("Error");
        throw OverflowingMailboxException ( "Ops");
    }
    if(size<0){
        throw UnderflowingMailboxException ( "Ops");
        throw logic_error("Error");
    }
    else my_Size=size;
}

int Mailbox::getContents(){
    return(my_Contents);
}

int Mailbox::deliverMail (int pieces)//improved deliverMail() handles the exceptions
{
    if(pieces<0){
        throw std:: logic_error("Eror");
    }
    else{
        my_Contents+=pieces;
        cout<<"The mailbox has "<<my_Contents<<" pieces of mail"<<endl;
        return(my_Contents);
    }
}

void Mailbox::pickupMail(){
    cout<<"You have picked up "<<my_Contents<<" pieces of mail from the mailbox"<<endl;
    my_Contents=0;
}


UnderflowingMailboxException::UnderflowingMailboxException(string const& my_string):logic_error(my_string)
{}

OverflowingMailboxException::OverflowingMailboxException(string const& my_string):logic_error(my_string)
{}