Skip to content

Instantly share code, notes, and snippets.

@castaneai
Created December 27, 2012 15:32
Show Gist options
  • Save castaneai/4389078 to your computer and use it in GitHub Desktop.
Save castaneai/4389078 to your computer and use it in GitHub Desktop.
二次元座標構造体
#include <ostream>
struct Point
{
public:
int x;
int y;
Point operator+(const Point&);
Point operator-(const Point&);
Point operator*(const int&);
Point operator/(const int&);
Point& operator+=(const Point&);
Point& operator-=(const Point&);
Point& operator*=(const int&);
Point& operator/=(const int&);
friend std::ostream& operator<<(std::ostream&, const Point&);
};
Point Point::operator+(const Point& right)
{
Point p = {this->x + right.x, this->y + right.y};
return p;
}
Point Point::operator-(const Point& right)
{
Point p = {this->x - right.x, this->y - right.y};
return p;
}
Point Point::operator*(const int& right)
{
Point p = {this->x * right, this->y * right};
return p;
}
Point Point::operator/(const int& right)
{
Point p = {this->x / right, this->y / right};
return p;
}
Point& Point::operator+=(const Point& right)
{
this->x += right.x;
this->y += right.y;
return *this;
}
Point& Point::operator-=(const Point& right)
{
this->x -= right.x;
this->y -= right.y;
return *this;
}
Point& Point::operator*=(const int& right)
{
this->x *= right;
this->y *= right;
return *this;
}
Point& Point::operator/=(const int& right)
{
this->x /= right;
this->y /= right;
return *this;
}
std::ostream& operator<<(std::ostream& out, const Point& p)
{
out << "(" << p.x << ", " << p.y << ")";
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment