//Add the implementation for == operation for Box class. #include <iostream> usi
ID: 665133 • 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;
}
show the ouput.
Explanation / Answer
working c++ code
#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 operator== (const Box &p);
};
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;
if ((this->x == p.x) && (this->y == p.y))
{
b.x=1;
b.y=1;
}
else
{
b.x=0;
b.y=0;
}
return b;
}
int main(){
Box b1(10,12);
Box b2(5,10);
Box b3; // b1 + b2;
b3 = b1 + b2;
Box b4(5,5);
Box b5(5,5);
Box b6;
b6=(b4==b5);
cout<<b6.x<<endl<<b6.y;
cout<<b3.x<<", "<< b3.y<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.