C++ Project 1: Pointer TrashCan Using the TrashCan class provided earlier, upgra
ID: 3570970 • Letter: C
Question
C++
Project 1: 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 <<( std::ostream& outs, const TrashCan * can );
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
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
Driver Code
#include "TrashCan.h"
#include
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
Note:
When a NULL value is assigned to the object, it is generating the break point. Which means a pointer can not refer to the NULL values.
Here, the added code is highlighted in bold letters. Please go through. The complete program code is followed by the sample output as shown below:
Sample Program:
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( );
//additionally added methods
friend TrashCan operator+(TrashCan c1, TrashCan c2);
friend TrashCan operator-(TrashCan c1, TrashCan c2);
friend ostream &operator<<(ostream &output, TrashCan* &c);
friend istream &operator>>(istream &input, TrashCan* &c);
bool operator>(TrashCan &c1)
{
bool flag=false;
int cont= this->getContents();
int cont1=c1.getContents();
if(cont>cont1)
flag=true;
return flag;
}
bool operator<(TrashCan &c1)
{
bool flag=false;
int cont= this->getContents();
int cont1=c1.getContents();
if(cont<cont1)
flag=true;
return flag;
}
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
#endif
-----------------------------------------------------------------------------------
TrashCan.cpp
#include "StdAfx.h"
#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;
}
//additionally added methods
//operator + overloading
TrashCan operator+(TrashCan c1, TrashCan c2)
{
int cont= c1.getContents();
int cont1=c2.getContents();
TrashCan t;
t.my_Contents=cont+cont1;
return t;
}
//operator - overloading
TrashCan operator-(TrashCan c1, TrashCan c2)
{
int cont= c1.getContents();
int cont1=c2.getContents();
TrashCan t;
t.my_Contents=cont-cont1;
return t;
}
//operator << overloading
ostream &operator<<(ostream &output, TrashCan* &c)
{
output << "A TrashCan with a size=" << c->getSize() << " and containing " <<c->getContents()<< " piece";
if (c->getContents() != 1) {
cout << "s";
}
output << " of trash" << endl;
return output;
}
//operator >> overloading
istream &operator>>(istream &input, TrashCan* &c)
{
int s;
int n;
cout<<"Size: ";
input>>s;
cout<<"Number of items to be added: ";
input>>n;
c->setSize(s);
for(int i=0;i<n;i++)
c->addItem();
return input;
}
// ThrashcanProgram.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#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= yourCan - 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;
}
// Code related to this assignment is below...
TrashCan * can = new TrashCan( );
can->setSize( 10 );
can->addItem( );
can->addItem( );
cout << can << endl;
// Now let's change the can...
cin >> can;
cout << can << endl;
delete( can );
TrashCan * nullcan = NULL;
// be careful...
cout << nullcan << endl;
cin >> nullcan;
system("pause");
return( 0 );
}
------------------------------------------------------------------------------------------------------
Sample output:
Welcome to Howie's TrashCan Program!
A TrashCan with a size=12 and containing 1 piece of trash
A TrashCan with a size=12 and containing 2 pieces of trash
this can's filled to 3
the other can's filled to 1
looks like combined is bigger...
looks like other is bigger...
looks like myCan is smaller...
A TrashCan with a size=10 and containing 2 pieces of trash
Size: 20
Number of items to be added: 7
A TrashCan with a size=20 and containing 9 pieces of trash
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.