Question: Point Class: Operator Overloading. How do you write the code for this? I'm having a hard time understanding operator overloading. The Point class represents a

Point Class: Operator Overloading.

How do you write the code for this? I'm having a hard time understanding operator overloading.

The Point class represents a point in the x-y plane, using integer coordinates x and y. Point objects can be added and subtracted, and the square of the norm of a point can be calculated (the square of the norm is computed rather than the norm, in order to perform all operations with integer arithmetic, and avoid the use of a square root function).

operator>> should read two ints from an input stream to set the values of members x and y. operator<

The provided header file is:

Point.h:

//

// Point.h

//

#ifndef POINT_H

#define POINT_H

#include

class Point

{

public:

Point(void) : x(0), y(0) {}

Point(int xin, int yin) : x(xin), y(yin) {}

int norm2(void) const { return x*x + y*y; }

Point operator+(const Point& rhs) const;

Point operator-(const Point& rhs) const;

int x, y;

};

std::ostream& operator<<(std::ostream& os, const Point& p);

std::istream& operator>>(std::istream& is, Point& p);

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!