7.c++基础知识_运算符重载_类外函数

1.运算符的重载

  • 加法重载
#include <stdio.h>
#include <iostream>

using namespace std;

class Point {
private:
	int x;
	int y;
public:
	Point(){
	
	}
	Point(int x,int y){
		this->x = x;
		this->y = y;
	}
	void setx(int x){
		this->x = x;
	}
	void sety(int y){
		this->y = y;
	}
	void printInfo(void){
		cout<<"x = "<<x<<" y = "<<y<<endl;
	}

	friend Point add(Point &p1,Point &p2);
	friend Point operator+(Point &p1,Point &p2);
};
Point add(Point &p1,Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

//加法的重载
Point operator+(Point &p1,Point &p2)
{
	cout<<"operator+"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}
int main()
{
	Point p1(1,2);
	Point p2(2,3);

	Point sum = p1 + p2;
	sum.printInfo();

	system("pause");
	return 0;
}
  • ++重载
#include <stdio.h>
#include <iostream>

using namespace std;

class Point {
private:
	int x;
	int y;
public:
	Point(){
	
	}
	Point(int x,int y){
		this->x = x;
		this->y = y;
	}
	void setx(int x){
		this->x = x;
	}
	void sety(int y){
		this->y = y;
	}
	void printInfo(void){
		cout<<"x = "<<x<<" y = "<<y<<endl;
	}

	friend Point add(Point &p1,Point &p2);
	friend Point operator+(Point &p1,Point &p2);
	friend Point operator++(Point &p);
	friend Point operator++(Point &p,int a);
};
Point add(Point &p1,Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

//加法的重载
Point operator+(Point &p1,Point &p2)
{
	cout<<"operator+"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}
//前++重载
Point operator++(Point &p)
{
	cout<<"operator++(Point &p)"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

//后++重载
Point operator++(Point &p,int a)
{
	cout<<"operator++(Point &p,int a)"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;
}
int main()
{
	Point p1(1,2);
	Point p2(2,3);

	Point n = ++p1;
	n.printInfo();
	p1.printInfo();

	cout<<"***********************"<<endl;

	Point m = p2++;
	m.printInfo();
	p2.printInfo();

	system("pause");
	return 0;
}
  • 返回引用
    前++ 可以直接返回引用,避免没必要的重新构造新类
    后++ 不可以发回引用,因为后++中定义了局部类,用完就释放了
    值返回效率比较低,引用返回效率高
#include <stdio.h>
#include <iostream>

using namespace std;

class Point {
private:
	int x;
	int y;
public:
	Point(){
		cout<<"Point()"<<endl;
	}
	Point(int x,int y){
		cout<<"Point(int x,int y)"<<endl;
		this->x = x;
		this->y = y;
	}
	Point(const Point& p){
		cout<<"Point(const Point& p)"<<endl;
		this->x = p.x;
		this->y = p.y;
	}
	~Point(){
		cout<<"~Point()"<<endl;
	}
	void setx(int x){
		this->x = x;
	}
	void sety(int y){
		this->y = y;
	}
	void printInfo(void){
		cout<<"x = "<<x<<" y = "<<y<<endl;
	}

	friend Point add(Point &p1,Point &p2);
	friend Point operator+(Point &p1,Point &p2);
	friend Point& operator++(Point &p);
	friend Point operator++(Point &p,int a);
};
Point add(Point &p1,Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

//加法的重载
Point operator+(Point &p1,Point &p2)
{
	cout<<"operator+"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}
//前++重载
Point& operator++(Point &p)
{
	cout<<"operator++(Point &p)"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

//后++重载
Point operator++(Point &p,int a)
{
	cout<<"operator++(Point &p,int a)"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;
}
int main()
{
	Point p1(1,2);

	cout<<"begin"<<endl;
	++p1;
	cout<<"***********************"<<endl;
	p1++;
	cout<<"end"<<endl;
	system("pause");
	return 0;
}
  • <<的重载
#include <stdio.h>
#include <iostream>

using namespace std;

class Point {
private:
	int x;
	int y;
public:
	Point(){
		cout<<"Point()"<<endl;
	}
	Point(int x,int y){
		cout<<"Point(int x,int y)"<<endl;
		this->x = x;
		this->y = y;
	}
	Point(const Point& p){
		cout<<"Point(const Point& p)"<<endl;
		this->x = p.x;
		this->y = p.y;
	}
	~Point(){
		cout<<"~Point()"<<endl;
	}
	void setx(int x){
		this->x = x;
	}
	void sety(int y){
		this->y = y;
	}
	void printInfo(void){
		cout<<"x = "<<x<<" y = "<<y<<endl;
	}

	friend Point add(Point &p1,Point &p2);
	friend Point operator+(Point &p1,Point &p2);
	friend Point& operator++(Point &p);
	friend Point operator++(Point &p,int a);
	friend ostream& operator<<(ostream &o,Point p);
};
Point add(Point &p1,Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

//加法的重载
Point operator+(Point &p1,Point &p2)
{
	cout<<"operator+"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}
//前++重载
Point& operator++(Point &p)
{
	cout<<"operator++(Point &p)"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

//后++重载
Point operator++(Point &p,int a)
{
	cout<<"operator++(Point &p,int a)"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;
}

ostream& operator<<(ostream &o,Point p)
{
	cout<<"("<<p.x<<", "<<p.y<<")";
	return o;
}
int main()
{
	Point p1(1,2);
	Point m,n;

	cout<<"begin"<<endl;
	m = ++p1;
	cout<<"***********************"<<endl;
	n = p1++;
	cout<<"end"<<endl;

	cout<<m<<" "<<n<<endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIOT技术栈

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值