Get premium membership and access questions with answers, video lessons as well as revision papers.

Overload the + operator for the coord class so that it is both a binary operator and unary operator. When it is used as a...

      

Overload the + operator for the coord class so that it is both a binary operator and unary operator. When it is used as a unary operator, have the + make any negative coordinate value positive.

  

Answers


Davis
//Overload the + relative to coord class.
#include
using namespace std;
class coord {
int x, y //coordinate values
public:
coord() {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+(); / plus
};
//Overload + relative to coord class.
coord::operator +(coord ob2)
{
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}Overload unary + for coord class.
coord coord::operator+()
{
if(x<0) x= -x;
if(y<0) y= -y;
return *this;
}
int main()
{
coord o1(10, 10), o2(-2, ;
int x, y;
o1 = o1 + o2; // addition
o1.get_(x, y);
cout << "(o1+o2) x: "<< x <<", y: "<< y << "\n";
o2 = +o2; //absolute value
o2.get_xy(x, y);
cout < "(+o2) x: "<< x <<" y: << y << "\n";
return 0;
}
Githiari answered the question on June 6, 2018 at 18:44


Next: What are differences between spirit duplication and ink stencil duplication?
Previous: Overload the - and / operators for the coord class using friend functions.

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


Learn High School English on YouTube

Related Questions