Question

Using C++, write the following program: The Point.h file declares the class and you will create...

Using C++, write the following program:

The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators

Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that returns the distance between two points & declarations for overloading the << (output), == (comparison), < (less than), + (plus) and - (minus) operators.)

When comparing two 2D points for this assignment use the dictionary order comparison

For all operators where you have to choose to either implement them as a member function or a non-member function, use the following rules of thumb to decide:

  1. If it is a unary operator, implement it as a member function.
  2. If a binary operator treats both operands equally (it leaves them unchanged), implement this operator as a non-member function.
  3. If a binary operator does not treat both of its operands equally (usually it will change its left operand), it might be useful to make it a member function of its left operand’s type, if it has to access the operand's private parts.

/*

* Point.h

*

*/

#ifndef POINT_H_

#define POINT_H_

#include <iostream>

class Point {

public:

/**

* Constructor and destructor

*/

Point();

Point(const double x, const double y);

virtual ~Point();

/**

* Get the x value

*/

double getX() const;

/**

* Get the y value

*/

double getY() const;

/**

* Return the distance between Points

*/

double slope(const Point &p);

/**

* Output the Point as (x, y) to an output stream

*/

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

/**

* Declare comparison relationships

*/

friend bool operator ==(const Point &lhs, const Point &rhs);

friend bool operator <(const Point &lhs, const Point &rhs);

/**

* Declare math operators

*/

friend Point operator +(const Point &lhs, const Point &rhs);

friend Point operator -(const Point &lhs, const Point &rhs);

private:

double x;

double y;

};

#endif /* POINT_H_ */

Homework Answers

Answer #1

Program plan: As per the algorithm is given, the code was written commented clearly. there are some other methods mentioned for implementation like the magnitude of the points, setter methods for point objects.

Mathematcal explanation:

Formulae of slope: take two points p1(x1,y1) ,p2(x2,y2)

then slope=(y2-y1)/(x2-x1)

Magnitude= square roots of (x*x)+(y*y)

Program:

//include directives
#include<math.h>
#include <iostream>

using namespace std;


//point class definition
class point
{
private:
double x, y;
public:
point ()           //default constructor
{
x = 0;
y = 0;
}
point (double a, double b)   //parameterised constructor
{
x = a;
y = b;
}
point (point & p1)       //parameterised constructor with object itself
{
x = p1.x;
y = p1.y;
}
//setter methods for coordinates
void setX (double a)
{
x = a;
}
void setY (double b)
{
y = b;
}
//getter methods for coordinates
double getX ()
{
return x;
}
double getY ()
{
return y;
}
//setter method for coordinates
void setXY (double a, double b)
{
x = a;
y = b;
}
//to get magnitude ogf the point   
double getMagnitude ()
{
return (sqrt ((x * x) + (y * y)));
}
/* normal pint function to print the point *
* void print() *
*{ *
* cout<<"\npoint is ("<<x<<","<<y<<")";*
*}*/
//getSlope method to calculate the slope
double getSlope (point & p2)
{
double xd = p2.x - x;
double yd = p2.y - y;
return (yd / xd);
}
//overload functions declared here
friend std::ostream & operator << (std::ostream &, const point &);
// Declaration of comparison relationships
friend bool operator == (const point & lhs, const point & rhs);
friend bool operator < (const point & lhs, const point & rhs);
// Declaration of math operators
friend point operator + (const point & lhs, const point & rhs);
friend point operator - (const point & lhs, const point & rhs);
//Destructor definition
~point ()
{
}
};               //End of point class

// Definition of overload output operator   
ostream & operator << (std::ostream & out, const point & p)
{
out << "(" << p.x;
out << "," << p.y << ")" << "\n";
}

//definition of equals operator overlaoding
bool operator == (const point & lhs, const point & rhs)
{
if ((lhs.x == rhs.x) && (lhs.y == rhs.y))
return 1;
else
return 0;
}

//definition of < operator overloading
bool operator < (const point & lhs, const point & rhs)
{
if ((lhs.x < rhs.x) && (lhs.y < rhs.y))
return 1;
else
return 0;
}

//definition of overloaded + operator
point operator + (const point & lhs, const point & rhs)
{
point p;
p.x = lhs.x + rhs.x;
p.y = lhs.y + rhs.y;
return p;
}

//definition of overloaded - operator
point operator - (const point & lhs, const point & rhs)
{
point p;
p.x = lhs.x + rhs.x;
p.y = lhs.y + rhs.y;
return p;
}

//main method definition
int main ()
{
//point class objects declared by using constructors defined in class
point p1;
point p2 (2, 3);
point p3 (p2);
point r;
double d;
p2.setXY (4, 5);
//calculating the magnitude   
d = p1.getMagnitude ();
cout << "\nmagnitude value of p1 and p2" << d;
//calling the getSlope method
cout << "\nSlope p2 and p3:" << p2.getSlope (p3);
//using overloaded operators to compare the points
if (p1 == p2)
cout << "\np1 and p2 are the same points";
if (p1 < p2)
cout << "\n p1 is lessthan point p2";
//using the math operators to add and subtract point objects
r = p1 + p2;
cout << "Addition of two points are : " << r;
r = p2 - p3;
cout << "Subtraction of two points are : " << r;
//Displaying the points directly by using overloaded extraction operator
cout << " the points are:" << p1 << p2 << p3;
return 0;
}

Sample output:

magnitude value of p1 and p20                                                                                                  

Slope p2 and p3:1                                                                                                              

p1 is lessthan point p2Addition of two points are : (4,5)                                                                     

Subtraction of two points are : (6,8)                                                                                          

the points are:(0,0)                                                                                                          

(4,5)                                                                                                                          

(2,3)

output run on TURBO CPP:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
-For the COMPLEX class listed on the next page write code to: a) overload the addition...
-For the COMPLEX class listed on the next page write code to: a) overload the addition operator for addition between two COMPLEX values. b) Create a constructor that uses a single parameter to initialize the real member of a COMPLEX and sets the imaginary portion to 0. c) Write the code to produce the insertion operator. #include <iostream> using namespace std; class COMPLEX{    double Re;    double Im; public:    COMPLEX(double r, double im);       COMPLEX(const COMPLEX &c)...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Rewrite the following C++ class into Java, don't forget to put main into the Student class....
Rewrite the following C++ class into Java, don't forget to put main into the Student class. class Student { std::string lname; std::string fname; int age; double gpa; Student(const std::string& lname, const std::string& fname, int age, double gpa); std::string lname() const; std::string fname() const; int age() const; double gpa() const; friend std::ostream& operator<<(std::ostream& os, const Student& st); }; int main(int argc, const char* argv[]);
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include <string> #include <ostream> using namespace std; class Book { private:     string title;     string author;     unsigned isbn;     double price; public: /**Constructors*/    Book();    Book(string t, string a, unsigned i, double p);     /**Access Functions*/     string get_title();     string get_author();     unsigned get_isbn();     double get_price();     /**Manipulation Procedures*/    void set_title(string t);     void set_author(string a);     void set_isbn(unsigned...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Must be C++ programming    The MyPoint class was created to model a point in a...
Must be C++ programming    The MyPoint class was created to model a point in a two-dimensional space. The MyPoint class has the properties x and y that represent x- and y-coordinates, two get functions for x and y, and the function for returning the distance between two points. Create a class named ThreeDPoint to model a point in a three-dimensional space. Let ThreeDPoint be derived from MyPoint with the following additional features: A data field named z that represents...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT