Create a class which stores x and y coordinates of a point using Overload.
#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();
}
Comments
Leave a comment