Overload the following operators for point objects: +, ==, ! =, <<, >>. Write a
ID: 3704255 • 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.
THE CPP FILE:
THE HEADER:
So I want you to do the instructions above for the CPP file and the Header to overload the operators which are explained above. Thank you
Explanation / Answer
1. newpoint.h
// FILE: newpoint.h
// CLASS PROVIDED: point (an ADT for a point on a two-dimensional plane)
//
// CONSTRUCTOR for the point class:
// point(double initial_x = 0.0, double initial_y = 0.0)
// Postcondition: The point has been set to (initial_x, initial_y).
//
// MODIFICATION MEMBER FUNCTIONS for the point class:
// void shift(double x_amount, double y_amount)
// Postcondition: The point has been moved by x_amount along the x axis
// and by y_amount along the y axis.
//
// void rotate90( )
// Postcondition: The point has been rotated clockwise 90 degrees.
//
// CONSTANT MEMBER FUNCTIONS for the point class:
// double get_x( ) const
// Postcondition: The value returned is the x coordinate of the point.
//
// double get_y( ) const
// Postcondition: The value returned is the y coordinate of the point.
//
// NONMEMBER FUNCTIONS for the point class:
// double distance(const point& p1, const point& p2)
// Postcondition: The value returned is the distance between p1 and p2.
//
// point middle(const point& p1, const point& p2)
// Postcondition: The point returned is halfway between p1 and p2.
//
//
#ifndef MAIN_SAVITCH_NEWPOINT_H
#define MAIN_SAVITCH_NEWPOINT_H
#include <iostream> // Provides ostream and istream
using namespace std;
class point
{
public:
// CONSTRUCTOR
point(double initial_x = 0.0, double initial_y = 0.0);
// MODIFICATION MEMBER FUNCTIONS
void shift(double x_amount, double y_amount);
void rotate90();
// CONSTANT MEMBER FUNCTIONS
double get_x() const { return x; }
double get_y() const { return y; }
// Overloading + operator
point operator+(point const &p);
// Overloading >> operator
friend istream& operator >> (istream& ins, point& target)
{
ins >> target.x >> target.y;
return ins;
}
private:
double x, y; // x and y coordinates of this point
};
// NONMEMBER FUNCTIONS for the point class
double distance(const point& p1, const point& p2);
point middle(const point& p1, const point& p2);
// Overloading == operator
bool operator==(const point& p1, const point& p2);
// Overloading != operator
bool operator !=(const point& p1, const point& p2);
// Overloading << operator
ostream& operator <<(ostream& outs, const point& source);
#endif
2. newpoint.cpp
//#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "newpoint.h"
using namespace std;
point::point(double initial_x, double initial_y)
{
x = initial_x; // Constructor sets point to a given position
y = initial_y;
}
void point::shift(double x_amount, double y_amount)
{
x += x_amount;
y += y_amount;
}
void point::rotate90()
{
double new_x;
double new_y;
new_x = y; // For a 90 degree clockwise rotation the new y is -1
new_y = -x; // times original x, and the new x is the original y
x = new_x;
y = new_y;
}
int rotations_needed(point p)
{
int answer;
answer = 0;
while ((p.get_x() < 0) || (p.get_y() < 0))
{
p.rotate90();
++answer;
}
return answer;
}
double distance(const point& p1, const point& p2)
// Library facilities used: cmath
{
double a, b, c_squared;
// Calculate differences in x and y coordinates
a = p1.get_x() - p2.get_x(); // Difference in x coordinates
b = p1.get_y() - p2.get_y(); // Difference in y coordinates
// Pythagorean Theorem to calculate square of distance between points
c_squared = a*a + b*b;
return sqrt(c_squared); // sqrt calculates square root (from math.h)
}
point middle(const point& p1, const point& p2)
{
double x_midpoint, y_midpoint;
// Compute the x and y midpoints
x_midpoint = (p1.get_x() + p2.get_x()) / 2;
y_midpoint = (p1.get_y() + p2.get_y()) / 2;
// Construct a new point and return it
point midpoint(x_midpoint, y_midpoint);
return midpoint;
}
point point::operator+ (point const &p)
{
point res(x + p.get_x(), y + p.get_y());
return res;
}
bool operator== (const point& p1, const point& p2)
{
if ((p1.get_x() == p2.get_x()) && (p1.get_y() == p2.get_y()))
{
return true;
}
return false;
}
bool operator!= (const point& p1, const point& p2)
{
if ((p1.get_x() != p2.get_x()) || (p1.get_y() != p2.get_y()))
{
return true;
}
return false;
}
ostream& operator << (ostream& outs, const point& source)
{
outs << "X: " << source.get_x() << ", Y: " << source.get_y();
return outs;
}
//Adding main method
int main()
{
point p1(1, 2), p2(3, 4);
point p3, p4;
p3 = p1 + p2;
cout << p3 << endl;
cout << "Enter the x and y: " << endl;
cin >> p4;
bool res = p1 != p4;
cout << res << endl;
bool res1 = p2 == p4;
cout << res1 << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.