运算:+, -, *, /, +=等等
比较:>, = ,<, ≥, ≤, ==, !=等
赋值:=
输入:>>
输出:<<
文章转载于https://blog.csdn.net/weixin_43899069/article/details/104442108
#include
#include
using namespace std;
struct Point {
int x, y;
Point(int x = 0, int y = 0): x(x), y(y) {}
//重载算术运算符(- * / +=同理)
Point operator + (const Point& A) {
x += A.x;
y += A.y;
return *this;
}
//重载比较运算符(< == != 同理) bool operator > (const Point& A) {
return x > A.x;
}
//重载赋值运算符
Point operator = (const Point& A) {
x = A.x;
y = A.y;
return *this;
}
//重载输入输出运算符
friend istream& operator >> (istream& in, Point& P) {
in >> P.x >> P.y;
return in;
}
//重载输出运算符
friend ostream& operator << (ostream&, const Point& P) {
cout << "(" << P.x << "," << P.y << ")";
return cout;
}
}point[10]; //结构体数组
int main()
{
//创建
Point a(5,4), b(4,2); //构造函数创建结构体
point[0].x = 2 ; point[0].y = 4; //结构体数组创建结构体
point[1].x = 1 ; point[1].y = 4;
//算术运算符
point[1] = point[1]+point[0];
//比较运算符
cout << (point[0]>point[1]) << endl;
//赋值运算符:
point[2] = point[1];
cout << point[2] << endl; //输入运算符 cin >> point[5];
//输出运算符
cout << point[5];
return 0;
}
