Answer to Question #273840 in C++ for sonu

Question #273840

Create a class which stores x and y coordinates of a point using Overload.


1
Expert's answer
2021-12-04T10:19:25-0500
#include <iostream>
#include <string>

class Point
{
  double x;
  double y;
   
public:
  Point (): x(0.0), y(0.0) {}
  Point (double x_, double y_)
  {
    this -> x = x_;
    this -> y = y_;
  }
  void set_x(double x_)
  {
    this -> x = x_;
  }
  void set_y(double y_)
  {
    this -> y = y_;
  }
  const double& get_x ()
  {
    return this -> x;
  }
  const double& get_y ()
  {
    return this -> y;
  }
   Point operator+ (Point& p)
  {
    double new_x = this -> x + p.x;
    double new_y = this -> y + p.y;
    return Point(new_x, new_y);
  }
  void display()
  {
    std::cout<< "X: "<<this -> x << " Y: " << this -> y << std::endl;
  }
};

int main()
{
  Point p1 (2.0, 3.0);
  Point p2 (4.0, 2.7);
  Point p3 = p1 + p2;
  p3.display();
  Point p4 = p3 + p2 + p1;
  p4.display();

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS