implement a Point class with the following private data Point and operator overl
ID: 3917323 • Letter: I
Question
implement a Point class with the following private data Point and operator overloading Implement a Point class with the following private data: doublex double_y Implement the following methods, some will be member functions and others will be global functions. A default constructor that sets the Point to 0,0 A two parameter constructor Accessors for the _x and _y value operator (Point&, Point&) operatorl-(Point&, Point&) a method that if a Point having a value of(835) was used in the expression 4 (3.5) the resultant expression would yield a new Point (12, 20) a method that f a Point having a value of (3.5) was used in the expression (3,5) 4 the resultant expression would yield a new Point (12, 20) nogeratrst Dgabio such that an imoking Point object that previously h he value of (a5) and kDouble was 4 the i modified to (12, 20) a operatorExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Point{
private:
double _x;
double _y;
public:
Point(){
_x = 0;
_y = 0;
}
Point(double x, double y){
_x = x;
_y = y;
}
double getX() const{
return _x;
}
double getY() const{
return _y;
}
Point operator *(double d) {
return Point(_x * d, _y * d);
}
Point& operator *=(double d){
_x *= d;
_y *= d;
return *this;
}
Point operator +(const Point &p2){
return Point(_x + p2._x, _y +p2._y);
}
bool operator < (const Point &p2){
double d1 = sqrt(_x * _x + _y * _y);
double d2 = sqrt(p2._x * p2._x + p2._y * p2._y);
if(d1 < d2)
return true;
else
return false;
}
bool operator > (const Point &p2){
double d1 = sqrt(_x * _x + _y * _y);
double d2 = sqrt(p2._x * p2._x + p2._y * p2._y);
if(d1 > d2)
return true;
else
return false;
}
friend ostream& operator << (ostream &out, const Point &p){
out << "(" << p._x << ", " << p._y << ")";
return out;
}
friend istream& operator >> (istream &in, Point &p){
in.ignore(10, '(');
in >> p._x ;
in.ignore(10, ',');
in >> p._y ;
in.ignore(10, ')');
return in;
}
};
bool operator ==(const Point& p1, const Point &p2){
if(p1.getX() == p2.getX() && p1.getY() == p2.getY())
return true;
else
return false;
}
bool operator !=(const Point& p1, const Point &p2){
return !(p1 == p2);
}
Point operator* (double d, Point p){
return p * d;
}
int main(){
Point p1, p2(3, 2);
cout << "Enter point p1, for e.g. (2,5): ";
cin >> p1;
cout << "p1: " << p1 << endl;
cout << "p2: " << p2 << endl;
cout << "p1 + p2: " << p1 + p2 << endl;
cout << "4 * p1 = " << 4 * p1 << endl;
cout << "p1 * 4 = " << p1 * 4 << endl;
cout << "changing p2 using p2 *= 5" << endl;
p2 *= 5;
cout << "p2: " << p2 << endl;
if(p1 == p2)
cout << "p1 is equal to p2" << endl;
if(p1 != p2)
cout << "p1 is NOT equal to p2" << endl;
if(p1 < p2)
cout << "p1 is less than p2" << endl;
else
cout << "p1 is NOT less than p2" << endl;
if(p1 > p2)
cout << "p1 is greater than p2" << endl;
else
cout << "p1 is NOT greater than p2" << endl;
}
output
------
Enter point p1, for e.g. (2,5): (5, 8)
p1: (5, 8)
p2: (3, 2)
p1 + p2: (8, 10)
4 * p1 = (20, 32)
p1 * 4 = (20, 32)
changing p2 using p2 *= 5
p2: (15, 10)
p1 is NOT equal to p2
p1 is less than p2
p1 is NOT greater than p2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.