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

//Add the implementation for == operation for Box class. #include <iostream> usi

ID: 665019 • Letter: #

Question

//Add the implementation for == operation for Box class.

#include <iostream>
using namespace std;

class Box
{
public:
   int x,y;
   Box(int a=0, int b=0)
   {
       x = a;
       y = b;
   }
   Box operator+ (const Box &);
};
Box Box::operator+ (const Box &p)
{
   Box b;
   b.x = this->x + p.x;
   b.y = this->y + p.y;

   return b;
}

int main(){
   Box b1(10,12);
   Box b2(5,10);
   Box b3;               // b1 + b2;
   b3 = b1 + b2;
   cout<<b3.x<<", "<< b3.y<<endl;
   system("pause");
   return 0;
}

Explanation / Answer

Box Box::operator= (const Box &p)
{
    Box b;
    b.x = this->x + p.x;
    b.y = this->y + p.y;

    return b;
}

class Box
{
public:
    int x,y;
    Box(int a=0, int b=0)
    {
        x = a;
        y = b;
    }
    Box operator+ (const Box &);
    Box operator= (const Box &);
};
Box Box::operator+ (const Box &p)
{
    Box b;
    b.x = this->x + p.x;
    b.y = this->y + p.y;

    return b;
}

Box Box::operator= (const Box &p)
{
    Box b;
    b.x = this->x + p.x;
    b.y = this->y + p.y;

    return b;
}

int main(){
    Box b1(10,12);
    Box b2(5,10);
    Box b3;                // b1 + b2;
    b3 = b1 + b2;
    cout<<b3.x<<", "<< b3.y<<endl;
    system("pause");
    return 0;
}