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

Overload the following operators for point objects: +, ==, ! =, <<, >>. Write a

ID: 3703716 • Letter: O

Question

Overload the following operators for point objects: +, ==, ! =, <<, >>. Write a suitable driver program to test your overloaded operators.

//Overload + as a member function of the point class:

point operator +(const point& p);
// Postcondition: The sum of p1 and p2 is returned.

//Overload ==, ! = and << (stream insertion-output)operators as non-member functions:

// bool operator ==(const point& p1, const point& p2);
// Postcondition: The return value is true if p1 and p2 are identical.//

// bool operator !=(const point& p1, const point& p2)
// Postcondition: The return value is true if p1 and p2 are not identical.//

// ostream& operator <<(ostream& outs, const point& source)
// Postcondition: The x and y coordinates of source have been
// written to outs. The return value is the ostream outs.

//Overload >> (stream extraction -input) as a friend function of the point class

// friend istream& operator >>(istream& ins, point& target);
// Postcondition: The x and y coordinates of target have been
// read from ins. The return value is the istream ins.

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

point.h
--------

#ifndef point_h
#define point_h

#include <iostream>
using namespace std;

class point
{
private:
int x, y;
public:
point();
point(int x, int y);

int getX() const;
int getY() const;
void setX(int x);
void setY(int y);

//Overload + as a member function of the point class:
point operator +(const point& p);
// Postcondition: The sum of p1 and p2 is returned.


//Overload >> (stream extraction -input) as a friend function of the point class

// Postcondition: The x and y coordinates of target have been
// read from ins. The return value is the istream ins.

friend istream& operator >>(istream& ins, point& target);


};


//Overload ==, ! = and << (stream insertion-output)operators as non-member functions:

bool operator ==(const point& p1, const point& p2);
// Postcondition: The return value is true if p1 and p2 are identical.//

bool operator !=(const point& p1, const point& p2);
// Postcondition: The return value is true if p1 and p2 are not identical.//


ostream& operator <<(ostream& outs, const point& source);
// Postcondition: The x and y coordinates of source have been
// written to outs. The return value is the ostream outs.


#endif /* point_h */


point.cpp
----------


#include "point.h"
point::point()
{
x = 0;
y = 0;
}
point::point(int x, int y)
{
this->x = x;
this->y = y;
}

int point::getX() const
{
return x;
}
int point::getY() const
{
return y;
}
void point::setX(int x)
{
this->x = x;
}
void point::setY(int y)
{
this->y = y;
}

//Overload + as a member function of the point class:
point point::operator +(const point& p)
// Postcondition: The sum of p1 and p2 is returned.
{
return point(x + p.x, y + p.y);
}


//Overload >> (stream extraction -input) as a friend function of the point class

// Postcondition: The x and y coordinates of target have been
// read from ins. The return value is the istream ins.

istream& operator >>(istream& ins, point& target)
{
ins >> target.x >> target.y;
return ins;
}

//Overload ==, ! = and << (stream insertion-output)operators as non-member functions:

bool operator ==(const point& p1, const point& p2)
// Postcondition: The return value is true if p1 and p2 are identical.//
{
if(p1.getX() == p2.getX() && p1.getY() == p2.getY())
return true;
else
return false;
}

bool operator !=(const point& p1, const point& p2)
// Postcondition: The return value is true if p1 and p2 are not identical.//
{
return !(p1 == p2);
}


ostream& operator <<(ostream& outs, const point& source)
// Postcondition: The x and y coordinates of source have been
// written to outs. The return value is the ostream outs.
{
outs << "(" << source.getX() << "," << source.getY() << ")";
return outs;
}


main.cpp
----------

#include <iostream>
#include "point.h"
using namespace std;

int main()
{
point p1, p2(5, 10), p3, p4;

cout << "point p1: " << p1 << endl;
cout << "point p2: " << p2 << endl;

cout << "Enter point p3: ";
cin >> p3;

cout << "point p3: " << p3 << endl;

if(p2 != p3)
cout << "p2 and p3 NOT are equal" << endl;
else
cout << "p2 and p3 are equal" << endl;

cout << "assigning p3 to p4" << endl;
p4 = p3;

cout << "point p4: " << p4 << endl;

if(p3 == p4)
cout << "p3 and p4 are equal" << endl;
else
cout << "p3 and p4 are NOT equal" << endl;

point p5 = p2 + p3;
cout << "sum of p2+p3: " << p5 << endl;

return 0;
}

output
point p1: (0,0)
point p2: (5,10)
Enter point p3: 3 4
point p3: (3,4)
p2 and p3 NOT are equal
assigning p3 to p4
point p4: (3,4)
p3 and p4 are equal
sum of p2+p3: (8,14)