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

Background: This assignment revisits the issue of operator overloading. In this

ID: 3661952 • Letter: B

Question

Background:

This assignment revisits the issue of operator overloading. In this assignment, you are asked to implement << and >>. These operators are fairly tricky because their parameters are unusual and the way you need to code them is not exactly straightforward. Please read over this issue in the book and review the online content so it is clear to you before you begin. In addition, this assignment starts dealing with pointers to classes. Students find pointers very challenging because code that uses pointer doesn't always work. It depends on whether the pointer is any good, pointing to a non-NULL value. If you choose to continue in the C++ track at SMC, you'll need to be very comfortable with pointers.

Project 12: Pointer TrashCan

Using the TrashCan class provided earlier (.NET 2012 XCode), 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 * drive );
friend std::istream& operator >>( std::istream& ins, TrashCan * & drive );

HINT: Be very careful to test for NULL...

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 );
TrashCan * ptrCan = new TrashCan( 0, 0 );
TrashCan * nullCan = NULL; // a pointer to anything can be NULL...

// let's work with the pointers...
cin >> ptrCan;
cin >> nullCan; // be careful...

cout << ptrCan << endl;
cout << nullCan << endl; // be careful...

  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 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;
  }

  // let's throw some exceptions...

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

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

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

  return( 0 );

}

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

#include "TrashCan.h"

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

int main();

class TrashCan

{

   friend ostream& operator << (ostream& const TrasnCan &,const TrashCan & );
   friend istream& operator >> (istream input&,const TrashCan &,const TrashCan & );
   friend bool operator == (const TrashCan &, const TrashCan &);
   friend TrashCan operator - (const TrashCan &, const TrashCan &);
   friend TrashCan operator + (const TrashCan &, const TrashCan &);
   friend TrashCan operator * (const TrashCan &, const TrashCan &);
   friend TrashCan operator / (const TrashCan &, const TrashCan &);
private:
   int my_size;
   int array[10][10];
   int my_Contents;
public:
   TrashCan(int);
   TrashCan(int,int);
   bool operator != (const TrashCan &) const;
   TrashCan operator + (const TrashCan &) const;
   const TrashCan & operator = (const TrashCan &);
TrashCan::TrashCan(const TrashCan &);
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;
};
TrashCan TrashCan:: operator = (const TrashCan & a) const
{
   TrashCan temp(my_size, my_Contents);

   for (int i = 0; i < a.my_size; i++)
       for (int j = 0; j < a.my_size; j++)
temp.array[i][j] = a.array[i][j] + array[i][j];

return temp;
}
TrashCan operator - (const TrashCan & a, const TrashCan & b)
{
TrashCan temp(int my_size, int my_Contents);

for (int i = 0; i < a.my_size; i++)
for (int j = 0; j < a.my_size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];

return temp;
}

TrashCan operator + (const TrashCan & a, const TrashCan & b)
{
TrashCan temp(int my_size, int my_Contents);

for (int i = 0; i < a.my_size; i++)
for (int j = 0; j < a.my_size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];

return temp;
}
TrashCan operator * (const TrashCan & a, const TrashCan & b)
{
TrashCan temp(int my_size, int my_Contents);

for (int i = 0; i < a.my_size; i++)
for (int j = 0; j < a.my_size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];

return temp;
}
TrashCan operator / (const TrashCan & a, const TrashCan & b)
{
TrashCan temp(int my_size, int my_Contents);

for (int i = 0; i < a.my_size; i++)
for (int j = 0; j < a.my_size; j++)
temp.array[i][j] = a.array[i][j] - array[i][j];

return temp;
}

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;
}

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;
}