学习目标:
理解运算符重载的含义
内容:
//路飞的代码模板
#include<iostream>
using namespace std;
class A {
public:
int x, y;
A(int _x,int _y):x(_x),y(_y){}
A():x(0),y(0){}
void print(int x) {
cout << x << " print " << y << endl;
}
void print(double a) {
cout << (double)x << "double " << (double)y << endl;
}
A operator+(const A& p) {
A t(this->x, this->y);
return A(t.x += p.x,t.y += p.y);
}
A& operator+=(const A& p) {
this->x += p.x;
this->y += p.y;
return *this;
}
double operator*(const A& p) {
return (this->x * p.x+ this->y * p.y);
}
A operator++(int) {
A p(this->x, this->y);
this->x++;
this->y++;
return p;
}
A& operator++() {
this->x++;
this->y++;
return *this;
}
friend ostream& operator <<(ostream&, const A&);
};
ostream& operator <<(ostream& os, const A& p) {
os << "x= " <<(double)p.x << " y= " << (double)p.y;
return os;
}
int main() {
A p2{32,12};
A p1{1,2};
cout <<"++p2 = " << ++p2 << endl;
cout << "p1 + p2 = " << p1 + p2 << endl;
cout << "p1 += p2 = " << (p1 += p2) << endl;
cout<< "p1++ = " << p1++ << endl;
cout << "p1 * p2 = " << p1 * p2 << endl;
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/7896ad1932404175815ef8f0383f470c.pn—