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

Project 1: Inherited TrashCan Using the TrashCan class provided earlier, upgrade

ID: 3768626 • Letter: P

Question

Project 1: Inherited TrashCan

Using the TrashCan class provided earlier, upgrade the class so that it throws subclasses of the exception class std::logic_error when the user does silly things. In the past, about all you could do when the user demanded silly operations was use cout to state your displeasure over making an overflowing Trashcan (one where its contents exceeded its size) or an underflowing TrashCan (one with a negative contents values). In the past, you blindly used std::logic_error. However now that you have seen inheritance, I'd like you to make your TrashCan class throw more specific exceptions at various times. For example, if while using operator-, you end up with a TrashCan with a negative contents, throw a UnderflowingTrashCanException back at the user. If while using operator+, you end up with a TrashCan with more contents than its size allows, throw a OverflowingTrashCanException at the user. A revised sample driver is shown below. Throw the right kind of subclasses exception anytime your user makes silly demands on you.

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.

TrashCan Class

void setSize( int size );
void addItem( );
void empty( );
void cover( );
void uncover( );

void printCan( );

Driver Code

#include <iostream>
#include <stdexcept>
#include "TrashCan.h"


int main( ) {

  using namespace std;

  using namespace cs52;

  cout << "Welcome to Howie's TrashCan Program!" << endl;

  TrashCan myCan;
  TrashCan yourCan;
  TrashCan empty( 0, 0 );

  yourCan.setSize( 12 );
  myCan.setSize( 12 );

  yourCan.addItem( );
  yourCan.addItem( );
  myCan.addItem( );

  myCan.printCan();
  yourCan.printCan();

  // read in a TrashCan...
  // the class designer for TrashCan (that's you!)
  // gets to decide which fields matter and should be read in
  cs52::TrashCan sample;
  cin >> sample;

  // print out a TrashCan...
  // the class designer for TrashCan (that's you!)
  // gets to decide which fields matter and should be printed
  cout << sample << endl;

  TrashCan combined = yourCan + myCan;
  cout << "this drive's filled to " << combined.getContents( ) << endl;

  TrashCan other = combined – myCan;
  cout << "the other cup's filled to " << other.getContents( ) << endl;

  if (combined > other) {
    cout << "looks like combined is bigger..." << endl;
  }
  else {
    cout << "looks like other is bigger..." << endl;
  }

  if (myCan > other) {
    cout << "looks like myCan is bigger..." << endl;
  }
  else {
    cout << "looks like other is bigger..." << endl;
  }

  if (yourCan < myCan) {
    cout << "looks like yourCan is smaller..." << endl;
  }
  else {
    cout << "looks like myCan is smaller..." << endl;
  }

  // let's throw some exceptions...

  try {
  empty = empty – combined;
  cout << "something not right here..." << endl;
  } catch( UnderflowingTrashCanException ) {
    // an exception should get thrown...
    // so the lines of code here should
    // be run, not the cout statement above...
cout << "underflowing exception was caught. moving on... << endl;
  } catch( std::logic_error ) {
    // the new kind of exception should be caught, not this one
    cout << "wrong kind of exception caught..." << endl;
  }


  try {
  empty.addItem( );
  cout << "something not right here..." << endl;
  } catch( OverflowingTrashCanException ) {
    // an exception should get thrown...
    // so the lines of code here should
    // be run, not the cout statement above...

cout << "overflowing exception was caught. moving on... << endl;
  } catch( std::logic_error ) {
    // the new kind of exception should be caught, not this one
    cout << "wrong kind of exception caught..." << endl;

  }


  try {
  cs52::TrashCan t( -1, -1 );
  cout << "something not right here..." << endl;
  } catch( UnderflowingTrashCanException ) {
    // an exception should get thrown...
    // so the lines of code here should
    // be run, not the cout statement above...
    cout << "underflowing exception was caught. moving on... << endl;

  } catch( std::logic_error ) {
    // the new kind of exception should be caught, not this one
    cout << "wrong kind of exception caught..." << endl;
  }

  return( 0 );

}

Project 2: Pet


The class Pet represents a domesticated animal. As you'll from its class diagram, it is a concrete (instantiatable) class which might represent a household animal. The class Dog is a much more specific kind of Pet that identifies an animal of canine variety. Following the diagrams show below, create the classes Pet and Dog. A sample driver has been provided to guide your efforts. IN ORDER TO RECEIVE FULL CREDIT, YOUR SUBCLASS MUST CALL ITS PARENT CONSTRUCTOR AS WELL AS REUSE ITS PARENT'S PROTECTED MEMBERS.

Pet( );
Pet( std::string name, std::string variety, std::string says );

std::string getName( ) const;
std::string getVariety( ) const;
virtual std::string speak( );

Dog( );
Dog( std::string name );

virtual std::string speak( );

// THESE ARE PROTECTED!!!
std::string myName;
std::string myVariety;
std::string howIspeak;

// BY DEFAULT, DOG'S ARE
// THE VARIETY "Canine" AND
// SPEAK BY SAYING "Bark"

Pet * p = new Pet( "Sunset", "Cat", "meow..." );
Dog * d = new Dog( "Muffin" );

cout << "The " << p->getVariety( ) << " " << p->getName( ) << " speaks by saying " << p->speak( ) << endl;

cout << "The " << d->getVariety( ) << " " << d->getName( ) << " speaks by saying " << d->speak( ) << endl;

TrashCan Class

TrashCan( );
TrashCan(int size );
TrashCan(int size, int contents );

void setSize( int size );
void addItem( );
void empty( );
void cover( );
void uncover( );

void printCan( );

bool myIsCovered;
int my_Size;
int my_Contents;

Explanation / Answer

trashcan.cpp

#include "trashcan.h"
#include <iostream>
#include <cstdlib>
using namespace std;
TrashCan::TrashCan( ) {
myIsCover = false;
Size = 0;
Contents = 0;
Empty = 0;
}
TrashCan::TrashCan( int size ) {
myIsCover = false;
Size = size;
Contents = 0;
Empty = 0;
}
TrashCan::TrashCan( int size, int contents, int empty ) {
myIsCover = false;
Size = size;
Contents = contents;
Empty = empty;
}
void TrashCan::setSize( int size11 ) {
if (size11 < 0 ){
    throw logic_error("The exception was caught. moving on...");
}
else{
Size = size11;
}
}
int TrashCan::getSize( ) {
return( Size );
}
int TrashCan::getContents( ) {
return( Contents );
}
void TrashCan::addItem( ) {
Contents = Contents + 1;
if (Contents > Size || Contents <Size){
    throw logic_error("The exception was caught. moving on...");
}
}
void TrashCan::empties( ) {
Contents = 0;
}
void TrashCan::cover( ) {
myIsCover = true;
}
void TrashCan::uncover( ) {
myIsCover= false;
}
void TrashCan::printCan( ) {
cout << "The TrashCan with a size=" << Size << " and containing " << Contents << " piece";
if (Contents != 1) {
    cout << "s";
}
cout << " of trash" << endl;
}
TrashCan operator+ ( const TrashCan& yourCan, const TrashCan& myCan ) {
    TrashCan combined;
combined.Contents = yourCan.Contents + myCan.Contents ;
    return( combined );
}
TrashCan operator- ( const TrashCan& combined, const TrashCan& myCan ){
TrashCan other;
TrashCan empty;
empty.Contents = -1;
if ( empty.Contents < 0 ){
    throw logic_error("The exception was caught. moving on...");
}
else
{
other.my_Contents = combined.Contents - myCan.Contents;
return (other );
}
}
bool operator> ( const TrashCan& myCan, const TrashCan& yourCan) {             
TrashCan combined;
combined.Contents = yourCan.Contents + myCan.Contents ;
TrashCan empty;
empty.Empty = 0;
TrashCan other;
other.Contents = combined.Contents - myCan.Contents;
return combined.Contents > other.Contents && myCan.Contents >     other.Contents;
}
bool operator< (const TrashCan& myCan, const TrashCan& yourCan ) {        
TrashCan combined;
combined.Contents = yourCan.Contents + myCan.Contents ;
TrashCan other;
other.Contents = combined.Contents - myCan.Contents;
if (yourCan.Contents < myCan.Contents ){
    return yourCan.Contents > myCan.Contents ; }
else {
    return yourCan.Contents < myCan.Contents;
}
}

pet.cpp
#include <iostream>
#include <cstdlib>
class Pets
{
public:
Pets( );
Pets( std::string petname, std::string petvariety, std::string petsays )
{

    Name=petname;
    Variety=petvariety;
    howIspeakas=petsays;
}

std::string getName( ) const
{
    return Name;
};
public:
std::string getVariety( ) const
{
    return Variety;
};

std::string speak() const
{

    return howIspeakas;
};

protected:
std::string Name;
std::string Variety;
std::string howIspeakas;
};

class Dog: Pets
{
public:
Dog( );
Dog( std::string petname );

virtual void speak( );
};

Dog * d = new Dog( "Muffin" );
cout << "The Dog " << d->getVariety( ) << " " << d->getName( ) << " speaks by saying the " << d->speak( ) << endl;
class Dog: public Pets