C++ projects need help (complete answer+.exe file for best answer) Better TrashC
ID: 3570919 • Letter: C
Question
C++ projects need help (complete answer+.exe file for best answer)
Better TrashCan
I have provided you with a sample class named TrashCan which has been diagrammed below and described in the online course content. You can acquire the source to the TrashCan class by clicking here (.NET 2012 XCode)**(content below)
TrashCan.h
#ifndef TRASHCAN_H
#define TRASHCAN_H
#include <iostream>
using namespace std;
class TrashCan {
public: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( );
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
#endif
TrashCan.cpp
#include "TrashCan.h"
TrashCan::TrashCan( ) {
myIsCovered = false;
my_Size = 0;
my_Contents = 0;
}
TrashCan::TrashCan( int size ) {
myIsCovered = false;
my_Size = size;
my_Contents = 0;
}
TrashCan::TrashCan( int size, int contents ) {
myIsCovered = false;
my_Size = size;
my_Contents = contents;
}
void TrashCan::setSize( int size ) {
my_Size = size;
}
int TrashCan::getSize( ) {
return( my_Size );
}
int TrashCan::getContents( ) {
return( my_Contents );
}
void TrashCan::addItem( ) {
my_Contents = my_Contents + 1;
}
void TrashCan::empty( ) {
my_Contents = 0;
}
void TrashCan::cover( ) {
myIsCovered = true;
}
void TrashCan::uncover( ) {
myIsCovered = false;
}
void TrashCan::printCan( ) {
cout << "A TrashCan with a size=" << my_Size<< " and containing " << my_Contents << " piece";
if (my_Contents != 1) {
cout << "s";
}
cout << " of trash" << endl;
}
For this unit, I'd like you to identify additional member variables (data) that would be appropriate for the class TrashCan. I would also like you to identify additional methods (functions) that would be appropriate for the class TrashCan. Update and resubmit the .h file for TrashCan with the new possible methods and members you have identified commented and explained. YOU ARE NOT REQUIRED TO CODE UP ANYTHING MORE THAN THE .H FILE.
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();
return( 0 );
}
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
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
New method that can be added in TrashCan.h file:
int IsFull(); // It will determine whether TrashCan is full or not, if it is full then new content will not be added.
// Definition of + method
TrashCan operator+(const TrashCan& other) {
return TrashCan(this.getSize() + other.getSize(), this.getContent()+other.getContent());
}
// Definition of - method
TrashCan operator-(const TrashCan& other) {
if(this.getSize() < other.getSize() || this.getContent() < other.getContent()) {
cout<<"Negative size of content"<<endl;
return null;
}
return TrashCan(this.getSize() + other.getSize(),this.getContent()+other.getContent());
}
int operator<(const TrashCan& other) {
if(this.getSize() < other.getSize()) return 1;
else return 0;
}
int operator>(const TrashCan& other) {
if(this.getSize() > other.getSize()) return 1;
else return 0;
}
friend std::ostream& operator <<( std::ostream& outs, const TrashCan * can ) {
outs << "Size of can : " << can->getSize() <<" Content in can: " << can->getContents() << endl;
return outs;
}
friend std::istream& operator >>( std::istream& ins, TrashCan * & can ){
int numItem, numContent;
ins >> numItem >> numContent;
can.setSize(numItem);
for(int i=0; i<numContent; i++) can.addItem();
return ins;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.