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

C++ Operator Overloading - Need this done in Xcode. The TrashCan class code is p

ID: 3579364 • Letter: C

Question

C++ Operator Overloading - Need this done in Xcode.

The TrashCan class code is provided at the bottom of this post.

TrashCan

void setSize( int size );
void addItem();
void empty();
void cover();
void uncover();

void printCan( );

TrashCan yours( 5, 3 );
TrashCan mine( 100, 3 );
yours.addItem( );
mine.addItem( );

cout << "----some tests follow----" << endl;

TrashCan eightItems = mine + mine;
TrashCan zeroItems = yours - yours;
eightItems.printCan();
zeroItems.printCan();
if (eightItems > zeroItems) {
cout << "> is working..." << endl;
}
if (eightItems != zeroItems) {
cout << "!= is working..." << endl;
}
if (zeroItems < eightItems) {
cout << "< is working..." << endl;
}
if (eightItems == eightItems) {
cout << "== is working..." << endl;
}

cout << "---some broken code follows---";
// each of the operator calls below should print out
// some kind of error message, instead of working correctly...

TrashCan negativeItems = mine - eightItems;

TrashCan overfull = yours + yours;

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

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

Implementation Details Sample Driver

TrashCan

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 mySize;
int myContents;

TrashCan yours( 5, 3 );
TrashCan mine( 100, 3 );
yours.addItem( );
mine.addItem( );

cout << "----some tests follow----" << endl;

TrashCan eightItems = mine + mine;
TrashCan zeroItems = yours - yours;
eightItems.printCan();
zeroItems.printCan();
if (eightItems > zeroItems) {
cout << "> is working..." << endl;
}
if (eightItems != zeroItems) {
cout << "!= is working..." << endl;
}
if (zeroItems < eightItems) {
cout << "< is working..." << endl;
}
if (eightItems == eightItems) {
cout << "== is working..." << endl;
}

cout << "---some broken code follows---";
// each of the operator calls below should print out
// some kind of error message, instead of working correctly...

TrashCan negativeItems = mine - eightItems;

TrashCan overfull = yours + yours;

Background: In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is redefinable in this way, but many of the ones we have encounted so far are, like and and and and and for example. It is a class' choice to do this, so mostly it is viewed as a driver code convenience to support them. But because so many of us have an assumption that should do addition but also perform string concatenation when working with textual data. Each operator becomes a friend function in C++ that your class can implement. The arguments to most of the operator overloads are defined as const Mailbox &. This syntax is a new kind of parameter passing called const-reference parameters. For a classtype, like Mailbox, const & signifies a read-only argument that cannot be changed by the function that receives this object. The compiler enforces this restriction and, for classes, const & simulates a pass-by-value mechanism without the overhead of copying the object, which might take a tremendous amount of time away from our program. Read the book and demo source examples carefully to see how this is done. Trust me, the first time you work with operators in C++, it is a very error-prone process. My best advice to you is to complete one operator at a time. Project 10: Fuller TrashCan I have provided you with a sample class named TrashCan which has been diagrammed below. You can acquire the source to the TrashCan class by clicking on the link for your development environment VS2012 XCode7 Using the TrashCan class provided earlier, upgrade the class so that it supports various operators. Make operator+ combine together the contents of two TrashCan, as long as the 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

Explanation / Answer

calculation = add(multiply(a, b),divide(a, b));
to

calculation = (a*b)+(a/b);
How to overload operators in C++ programming?
To overload AN operator, a special operator operate is outlined within the category as:

class categoryName
image (arguments)
  
... .. ...
};
Here, come backType is that the return style of the operate.
The returnType of the operate is followed by operator keyword.
Symbol is that the operator image you wish to overload. Like: +, &lt;, -, ++
You can pass arguments to the operator operate in similar approach as functions.
Example: Operator overloading in C++ Programming
#include &lt;iostream&gt;
using namespace std;

class Test


void operator ++()

void Display()
};

int main()
{
Test t;
// this calls "function void operator ++()" operate
++t;
t.Display();
return 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