Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

// FILE: newpoint.cpp // CLASS IMPLEMENTED: point (See newpoint.h for documentation.) #include  #include  #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; } 

THE HEADER:

// 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  // Provides ostream and istream 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; } 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); #endif 

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. What would you do first if you were Jennifer?

Answered: 1 week ago