Project 1: Fuller TrashCan Using TrashCan.cpp (.NET 2012 XCode ), enhance the Tr
ID: 3768618 • Letter: P
Question
Project 1: Fuller TrashCan
Using TrashCan.cpp (.NET 2012 XCode ), enhance the TrashCan class so that it supports the operators +, -, < and >. A sample pile of driver code is shown below to assist you in this effort. Operators + and - should create a new TrashCan from the two arguments by combining their contents. If you wind up with a TrashCan with a size that exceeds its capacity, print out an error message. If you wind up with a negative capacity or negative size, print out an error message. Operators < and > must return bool and should compare the holdings of the two arguments to determine which one is bigger.
My strong advice is to work one operator at a time, as these steps are very error-prone and lead to many, many compile errors.
TrashCan Class
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
Driver Code
#include "TrashCan.h"
#include <iostream>
using namespace std;
int main( ) {
cout << "Welcome to Howie's TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
TrashCan combined = yourCan + myCan;
cout << "this can's filled to " << combined.getContents( ) << endl;
TrashCan other = combined – myCan;
cout << "the other can'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;
}
return( 0 );
}
TrashCan Class
TrashCan( );TrashCan(int size );
TrashCan(int size, int contents );
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
bool myIsCovered;int my_Size;
int my_Contents;
Explanation / Answer
Below are the two working overloading operator for your trash class. We need to overload + and - operator to make it work as specified in the problem. Based on the UML the implentation of the operators will look like as shown below:
for + operator code will be
TrashCan operator+(const TrashCan& b)
{
TrashCan comb;
comb.size = this->size+ b.size;
if (comb.size > 20) ///assuming max capacity is 20 here
cout<<"you have crossed the maximum size of the can"<<endl;
return comb;
}
for - operator code will be
TrashCan operator-(const TrashCan& b1)
{
TrashCan comb;
comb.size = this->size- b1.size;
if(comb.size < 0)
cout<<"output of this operation is negative"<<endl;
return comb;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.