i'm having trouble with this assignment for my cs class the directions say "Make operator+ combine together the contents of two TrashCan, as long as the contents does not exceed the size. Make operator- subtract one TrashCans 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"
i typed the code out but i dont have and idea on how to input the operaators in code

#include <iostream>
#include <iomanip>
using namespace std;
int main();
{
TrashCan();
TrashCan( int size );
TrashCan( int size, int contents );
int main ();
void setSize( int size );
void addItem( );
void empty( ); 
void cover( );
void uncover( );
void printCan( );
bool myIsCovered;

int my_Size;
int my_Contents;
TrashCan myCan;
TrashCan yourCan;
TrashCan bigCan;
TrashCan junk;
TrashCan otherCan;
bigCan.setSize( 128 );
yourCan.setSize( 12 );
myCan.setSize( 12 );
junk.setSize( 1 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
junk.addItem();

junk.addItem();
otherCan = yourCan + myCan;

otherCan = junk - bigCan;

cout << junk << endl;

if (junk > bigCan) {
cout << "junk is bigger" << endl;
}
if (junk >= bigCan) {
cout << "junk is bigger or equal" << endl;
}
if (myCan < junk) {
cout<< "myCan is smaller" <<endl;
}
if (myCan <= junk) {
cout<< "myCan is smaller or equal" <<endl;
}
if (myCan != yourCan) {
cout << "myCan != yourCan!" <<endl;
}
if (myCan == myCan) {
cout << "equal test works" <<endl;
return 0;
}

This task is all about operator overloading. Search for "C++ operator overloading" in your favourite search engine and you'll get lots of examples. You first need to make a TrashCan class though, have you done that?

As ravenous pointed out, this assignment is about operator overloading. You'll need a TrashCan class where you specifically overload the operators, here you'll need to overload operator +, -, >> and <<, plus the boolean operators.
So, first of all you'll need a class:

class TrashCan{
    //class stuff
};

and in that class you'll need to insert functions that will overload the operators:

class TrashCan{
    int size;
    //class stuff;
public:
    TrashCan(int size){
        this->size=size;
    }

    int getSize(){ return size;}
    void setSize(int s){ size=s;}

    TrashCan operator+(TrashCan &obj){
        obj.setSize(obj.getSize()+this->getSize());
        return obj;
    }
};

Do that for every operator. Here are some usefull links:
Click Here
Click Here

i've reworte the code with class and functions but i dont know it its still correct.

#include "TrashCan.h"
#include <iostream>
using namespace std;

int main();
class TrashCan
{
    friend ostream& operator << (ostream& const TrasnCan&);
    friend bool 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 :: 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(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::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;
}

So i wrote the my program again with all operators defined but im still having trouble running it

#include <"trashcan2.h">
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main();
class TrashCan
{
    friend ostream& operator << (ostream& const TrasnCan&);
    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 :: 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(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(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(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(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::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;
}

its giving me errors codes of having words: my_size, my_content, array, myIsCovered as undeclared identifier, and
cpp(9) : see declaration of 'TrashCan'
cpp(10): error C2143: syntax error : missing ',' before '&'
cpp(10): error C2805: binary 'operator <<' has too few parameters
cpp(29): error C2511: 'TrashCan TrashCan::operator =(const TrashCan &) const' : overloaded member function not found in 'TrashCan'
cpp(118): error C2039: 'printCan' : is not a member of 'TrashCan'

cpp(80): error C2511: 'TrashCan::TrashCan(void)' : overloaded member function not found in 'TrashCan'

you need to move main to after the class. line 10 you are missing a , between the ostream& and the trashcan&. For your forth error lines 25 and 28 do not match. Your fifth error is that you dont have printCan in you class decleration. That goes for that last error as well

i tried to do what you said but i can't seem to get my code work

//#include <"trashcan2.h">
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

class TrashCan
{
    friend ostream& operator << (ostream&, 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;
    TrashCan operator - (const TrashCan &) const;
    TrashCan operator * (const TrashCan &) const;
    TrashCan operator / (const TrashCan &) const;
    const TrashCan TrashCan :: operator = (const TrashCan &);
};

int main();
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(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(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(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(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();
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;
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;
}
This article has been dead for over six months. Start a new discussion instead.