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

C++ Program Using the TrashCan class provided: TrashCan class provided: #include

ID: 3777508 • Letter: C

Question

C++ Program

Using the TrashCan class provided:

TrashCan class provided:

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

TrashCan header for the TrashCan class provided above:

#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

upgrade the class so that it supports various operators. Make operato combine together the contents of two TrashCan, as long as the r+ contents does not exceed the size. Make operator- subtract one TrashCan contents from another, as long as the size or contents don't go negative. Support the and operators to allow instances to be read from cin or written to cout. Make the boolean operators and test TrashCan's contents. Make the boolean operator and test all of the data members of a TrashCan for an exact match Implementation Details Sample Driver TrashCan yours 5, 3 TrashCan mine 100 3 yours. addItem mine addItem i TrashCan cout --some tests follow----" endl; TrashCan TrashCan eightItems mine mine Trashcan int size Trashcan zero Items Trashcan Yours Yours int size int contents eight Items printCan void setSize int size zeroItems printCan void add Item if (eight Items zeroItems) void empty cout is working enal void cover void uncover if (eight Items zero It em S s

Explanation / Answer

PROGRAM CODE:

Header file:

/*
* TrashCan.h
*
* Created on: 26-Nov-2016
* Author: kasturi
*/

#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( );
TrashCan operator+(TrashCan tc);
TrashCan operator-(TrashCan tc);
friend ostream& operator<<(ostream &out, TrashCan const &can);
friend istream& operator>>(istream &in, TrashCan &can);
bool operator<(TrashCan tc);
bool operator>(TrashCan tc);
bool operator<=(TrashCan tc);
bool operator>=(TrashCan tc);
bool operator!=(TrashCan tc);
bool operator==(TrashCan tc);
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
#endif

CPP file:

/*
* TrashCan.cpp
*
* Created on: 26-Nov-2016
* Author: kasturi
*/

#include "TrashCan.h"
#include <iostream>
using namespace std;
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;
}

//operator functions for the class
TrashCan TrashCan::operator+( TrashCan tc) {

   TrashCan tc1;
   if(this->my_Contents + tc.my_Contents <= this->my_Size)
{
       tc1.my_Contents = this->my_Contents + tc.my_Contents;
       tc1.my_Size = this->my_Contents + tc.my_Contents;
}
return tc1;
}

TrashCan TrashCan::operator-( TrashCan tc) {

   TrashCan tc1;
if(this->my_Contents - tc.my_Contents >= 0)
{
   tc1.my_Contents = this->my_Contents - tc.my_Contents;
   tc1.my_Size = this->my_Contents - tc.my_Contents;
}
return tc1;
}

std::ostream& operator<<(ostream &out, TrashCan const &tc) {
   out << "A TrashCan with a size=" << tc.my_Size << " and containing " << tc.my_Contents << " piece";
   if (tc.my_Contents != 1) {
   out << "s";
   }
   out << " of trash" << endl;
   return out;
}

std::istream& operator>>(istream &in, TrashCan &tc)
{
   in>>tc.my_Size>>tc.my_Contents;
   return in;
}

bool TrashCan::operator<(TrashCan tc)
{
   if(this->my_Size < tc.my_Size && this->my_Contents < tc.my_Contents )
       return true;
   else return false;
}

bool TrashCan::operator>(TrashCan tc)
{
   if(this->my_Size > tc.my_Size && this->my_Contents > tc.my_Contents)
       return true;
   else return false;
}

bool TrashCan::operator<=(TrashCan tc)
{
   if(this->my_Size <= tc.my_Size && this->my_Contents <= tc.my_Contents)
       return true;
   else return false;
}

bool TrashCan::operator>=(TrashCan tc)
{
   if(this->my_Size >= tc.my_Size && this->my_Contents >= tc.my_Contents)
       return true;
   else return false;
}

bool TrashCan::operator!=(TrashCan tc)
{
   if(this->my_Size != tc.my_Size && this->my_Contents != tc.my_Contents && this->myIsCovered != tc.myIsCovered)
       return true;
   else return false;
}

bool TrashCan::operator==(TrashCan tc)
{
   if(this->my_Size == tc.my_Size && this->my_Contents == tc.my_Contents && this->myIsCovered == tc.myIsCovered)
       return true;
   else return false;
}

int main()
{
   TrashCan tc1;
   tc1.setSize(10);
   tc1.addItem();
   tc1.addItem();

   TrashCan tc2;
   tc2.setSize(1);
   tc2.addItem();

   TrashCan tc3;
   tc3.setSize(10);
   tc3.addItem();
   tc3.addItem();

   cout<<to_string(tc3==tc1)<<endl;
   cout<<to_string(tc2<tc3)<<endl;
   cout<<tc2;
   cout<<to_string(tc2>tc3)<<endl;
   cout<<to_string(tc2<=tc3)<<endl;
   cout<<to_string(tc2>=tc3)<<endl;
   cout<<to_string(tc2!=tc3)<<endl;
}

OUTPUT:

1

1

A TrashCan with a size=1 and containing 1 piece of trash

0

1

0

0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote