Overload the >> and << shift operators relative to the coord class so that the following types of operations are allowed: ob << integer ob >> integer make sure your operators shift the x and y values by the amount specified.

      

Overload the >> and << shift operators relative to the coord class so that the following types of operations are allowed:
ob << integer
ob >> integer
make sure your operators shift the x and y values by the amount specified.

  

Answers


Davis

#include
using namespace std;
class coord {
int x, y;
public:
cord() {x=0; y=0;}
coord(int i, int j) {x=i; y=j;}
void get_xy(int &i, int &j) {i=x; j=y;}
coord operator<<(int i);
coord operator>>(int i);Overload <<.
coord coord::operator <<(int i)
{
coord temp;
temp.x = x << i;
temp.y = y << i;
return temp;
}
//Overload >>.
coord coord::operator >>(i i)
{
coord temp;
temp.x = x>> i;
temp.y = y>> i;
return temp;
}
int main()
{
coord o1(4, 4) o2;
int x, y;
o2 = o1 << 2;
o2.get_xy(x, y);
cout <o2 = o1>> 2;
o2.get_xy(x, y);
cout << "(o1>>2) x: "<return 0;
Githiari answered the question on June 8, 2018 at 19:23


Next: Given the class below, class three_d{ int x, y, z; three_(int i, int j, int k) { x = i; y = j; z = k; } three_() {x=0; y=0;...
Previous: Create a function called reverse() that takes two parameters. The first parameter called str, is a pointer to a string that will be reversed upon...

View More Computer Science Questions and Answers | Return to Questions Index


Exams With Marking Schemes

Related Questions