C++的运算符重载(二)加号、减号、复制、加等于、等于、不等于、负号、 左移、右移、前置++、后置++等的重载

关于运算符重载的相关介绍和注意事项在上一篇博客上已经讲解了,这里就不再赘述了。

话不多说,直接上代码

#include <iostream>
using namespace std;

class Point {
private:
	int m_x;
	int m_y;
public:
	Point(){}
	Point(int x,int y):m_x(x),m_y(y){}
	void Display(){
		cout << "(" << m_x << "," << m_y << ")" <<endl;
	}

	Point(const Point &point){
		m_x=point.m_x;
		m_y=point.m_y;
	}

	 //加号重载
	const Point operator+(const Point &point) const {
		return Point(m_x+point.m_x,m_y+point.m_y);
	}

	// 减号重载
	const Point operator-(const Point &point) const {
		return Point(m_x-point.m_x,m_y-point.m_y);
	}

	// 等于号重载
	Point& operator= (Point& point){
		m_x=point.m_x;
		m_x=point.m_x;
		return *this;
	}

	// +=重载
	Point &operator+=(const Point &point) {
		m_x += point.m_x;
		m_y += point.m_y;
		return *this;
	}

	// ==重载
	bool operator==(const Point &point) const {
		return (m_x == point.m_x) && (m_y == point.m_y);
	}

	// !=重载
	bool operator!=(const Point &point) const {
		return (m_x != point.m_x) || (m_y != point.m_y);
	}

	// 负号重载
	const Point operator-() const {
		return Point(-m_x, -m_y);
	}

	// 前置++
	Point &operator++(){
		m_x++;
		m_y++;
		return *this;
	}
	
	// 后置++
	const Point &operator++(int){
		Point old(m_x,m_y);
		m_x++;
		m_y++;
		return old;
	}

	friend ostream &operator << (ostream &,const Point &);
	friend istream &operator >> (istream &cin, Point &point) ;
};

ostream &operator << (ostream &cout,const Point &point) {
	cout << "(" << point.m_x << "," << point.m_y << ")"<< endl ;
	return cout;
}

istream &operator >> (istream &cin, Point &point) {
	cin >> point.m_x;
	cin >> point.m_y;
	return cin;
}

int main(){
	Point p0(10,20);
	Point p1(10,10);
	Point p2(10,20);
	Point p3(30,30);

	cout << -p0;

	Point p4=p1;
	p4.Display();

	Point p5=p0+=p1;
	p5.Display();

	Point p6=p1+p2+p3;
	p6.Display();

	Point p7=p1+p2-p3;
	p7.Display();

	cout << (p0==p1) << endl; //p0==p1,返回1,否则返回0
	cout << (p0!=p1) << endl; //p0!=p1,返回1,否则返回0

	Point p10(100,100);
	Point p11(200,200);
	cout << p10++ + p11;
	cout << ++p10+ p11;

	Point p8;
	cin >> p8;//从键盘输入
	cout << p8;

	return 0;
}

运行结果

(-10,-20)
(10,10)
(20,30)
(50,60)
(-10,0)
0
1
(300,300)
(302,302)
1000  1000 //从键盘输入的
(1000,1000)

=====================================================================
下面讲一些程序中的小细节

	const Point operator+(const Point &point) const {
		return Point(m_x+point.m_x,m_y+point.m_y);
	}
  • 第一个 const :
    我们都知道,两个数相加之后是不可以被赋值的,所以这个const是保证我的返回值是一个const对象,不能再对这个返回值赋值
  • 最后一个const :
    保证可以执行连续相加的操作(p1+p2+p3),在前面的博客中提到过,const对象是不能调用非const函数的,如果不加const ,p1+p1的返回值就不能够调用operator+函数了
	Point &operator+=(const Point &point) {
		m_x += point.m_x;
		m_y += point.m_y;
		return *this;
	}
  • 这里为什么要返回*this
    首先这个函数返回的是一个引用,也就是把它当成对象来使用,返回引用不就是返回对象,返回 *this不就是返回this所指向的内容。至于这里为什么要用引用?防止产生中间对象

再分享一个运算重载(用户输入两个复数,输出两个复数相加减的结果)

class Complex{
private:
	int r,v;
public:
	Complex(){}
	Complex (int r,int v):r(r),v(v){}

	Complex& operator+(Complex& c){
		int r=this->r+c.r;
		int v=this->v+c.v;
		static Complex ct(r,v);
		return ct;
	}

	//minus
	friend Complex& operator-(const Complex&,const Complex&);

	//output
	friend ostream& operator << (ostream& ,const Complex&);

	//input
	friend istream& operator >> (istream& , Complex&);
};


Complex& operator-(const Complex& c1,const Complex& c2){
	int r=c1.r-c2.r;
	int v=c1.v-c2.v;
	static Complex ct(r,v);
	return ct;
}

ostream& operator<<(ostream& os,const Complex& c){
	int r=c.r;
	int v=c.v;
	if(r==0){
		os << v << " ";
	}else if(v==0){
		os << r;
	}else if (v<0){
		os << r << v << "i";
	}else {
		os << r << "+" << v <<"i";
	}
	return os;
}

istream& operator >> (istream& is,  Complex& c){
	int r,v;
	scanf("%d%di",&r,&v);
	getchar();
	c.r=r;
	c.v=v;
	return is;
}

int main(){
	Complex c1,c2;
	cout << "input two number:" << endl;

	cin >> c1 >> c2;
	cout << "=========" <<endl;
	cout <<"c1+c2=" << (c1+c2) <<endl;
	cout <<"c1-c2=" << (c1-c2) <<endl;

	return 0;
}
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值