c++几种常见运算符重载

学习目标:

理解运算符重载的含义


内容:

//路飞的代码模板
#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—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值