The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 2 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Project 2: 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
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
if (my_Contents != 1) {
cout
}
cout
}
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 |
| TrashCan( ); TrashCan(int size ); TrashCan(int size, int contents ); void setSize( int size ); void printCan( ); |
| bool myIsCovered; int my_Size; int my_Contents; |
|
Driver Code |
|
#include "TrashCan.h" #include int main( ) { cout TrashCan myCan; yourCan.setSize( 12 ); yourCan.addItem( ); myCan.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 |
| TrashCan( ); TrashCan(int size ); TrashCan(int size, int contents ); void setSize( int size ); void printCan( ); |
| bool myIsCovered; int my_Size; int my_Contents; |
|
Driver Code |
|
#include "TrashCan.h" #include int main( ) { cout TrashCan myCan; yourCan.setSize( 12 ); yourCan.addItem( ); myCan.printCan(); TrashCan combined = yourCan + myCan; if (combined > other) { if (yourCan return( 0 ); } |
Project 3: Pointer TrashCan
Using the TrashCan class provided earlier, upgrade the class so that operator << and operator >> work well with pointers (that is, TrashCan *). Youll need to re-overload these operators, adding the function:
friend std::ostream& operator
friend std::istream& operator >>( std::istream& ins, TrashCan * & can );
HINT: Be very careful to test for NULL!
ANOTHER HINT: 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 |
| TrashCan( ); TrashCan(int size ); TrashCan(int size, int contents ); void setSize( int size ); void printCan( ); |
| bool myIsCovered; int my_Size; int my_Contents; |
|
Driver Code |
|
#include "TrashCan.h" #include int main( ) { cout TrashCan myCan; yourCan.setSize( 12 ); yourCan.addItem( ); myCan.printCan(); TrashCan combined = yourCan + myCan; if (combined > other) { if (yourCan // Code related to this assignment is below... delete( can ); TrashCan * nullcan = NULL; // be careful... cout return( 0 ); } |
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll