运算符重载

1.重载’+'运算符

为什么要重载加法运算符?

因为C++提供的加法运算符只能满足基本数据类型间的加法,如果我想让俩个相同的类的对象进行加法的话会报错

image-20231218154406227

所以为了能让俩个相同类的对象进行加法,我们要把这个过程封装到一个函数里面,只不过要多加一个关键字operator而已,让编译器一下子就找到,这个是重载运算符的函数

作用:实现俩个自定义运算符相加

成员函数实现运算符重载

可以少传递一个参数

#include <iostream>

using namespace std;

class Box {
	int length;
	int width;
	int height;
public:
	Box() {
		length = 0;
		width = 0;
		height = 0;
	}
	Box(int length,int width,int height) {
		this->length = length;
		this->width = width;
		this->height = height;
	}
	Box(const Box& other) {
		length = other.length;
		width = other.width;
		height = other.height;
	}
	//成员函数重载加法运算符
	Box operator+(const Box& other) {
		Box p;
		p.length = this->length + other.length;
		p.width = this->width + other.width; 
		p.height = this->height + other.height;
		return p;
	}
};
int main() {
	Box a(1,2,3);
	Box b(2, 3, 4);
	Box c = a + b;//直接调用
	Box d;
	d = a.operator+(b);//调用重载运算符的函数
	return 0;
}

全局函数实现运算符的重载

#include <iostream>

using namespace std;

class Box {
	int length;
	int width;
	int height;
	friend Box operator+(const Box& other1, const Box& other2);
	friend Box operator+(const Box& other1, int val);
public:
	Box() {
		length = 0;
		width = 0;
		height = 0;
	}
	Box(int length,int width,int height) {
		this->length = length;
		this->width = width;
		this->height = height;
	}
	Box(const Box& other) {
		length = other.length;
		width = other.width;
		height = other.height;
	}
};
//全局函数重载加法运算符
Box operator+(const Box& other1,const Box& other2) {//不调用成员函数是无法访问私有的成员变量的,需要设置为友元,告诉编译器,我这个重载运算符的函数是你这个类的好朋友,都哥们,能f
	Box p;
	p.length = other1.length + other2.length;
	p.width = other1.width + other2.width;
	p.height = other1.height + other2.height;
	return p;
}
Box operator+(const Box& other1,int val) {
	Box p;
	p.length = other1.length + val;
	p.width = other1.width + val;
	p.height = other1.height + val;
	return p;
}
int main() {
	Box a(1,2,3);
	Box b(2, 3, 4);
	Box c = a + b;
	Box d;
	d=operator+(a,b);
	return 0;
}

2.重载’+=’ 运算符

#include <iostream>

using namespace std;
class Box {
	int length;
	int width;
	int high;
	friend Box& operator+=(Box& other1, Box& other2);
public:
	Box() {
		length = 1;
		width = 2;
		high = 3;
	}
	/*Box& operator+=(const Box& other) {
		this->length += other.length;
		this->width += other.width;
		this->high += other.high;
		return *this;
	}*/
	int get_length() {
		return this->length;
	}
};
Box& operator+=(Box& other1,Box& other2) {
	other1.length += other2.length;
	other1.width += other2.width;
	other1.high += other2.high;
	return other1;
}
int main() {
	Box a, b,c;
	a += b+=c;//隐式调用函数
	cout << a.get_length();
	return 0;
}

3.重载’<<'运算符

cout是ostream类的对象

cin是istream类的对象

#include <iostream>

using namespace std;
class Box {
	int length;
	int width;
	int high;
	friend ostream& operator<<(ostream& o, const Box& b);
public:
	Box() {
		length = 1;
		width = 2;
		high = 3;
	}
};
ostream& operator<<(ostream& o,const Box& b) {
	o << b.length << ' ' << b.width << ' ' << b.high << endl;
	return o;
}
int main() {
	Box a, b,c;
	//cout << a;没有与这些操作数相匹配的运算符
	/*
	* 你想重载一个运算符要么在类内重载,要么在类外重载
	* 但是cout对象属于ostream类,该类我们无法修改,所以只能在类外用全局函数重载
	*/
	cout << a << b << c;
	return 0;
}

4.重载’>>'运算符

#include <iostream>

using namespace std;
class Box {
	int length;
	int width;
	int high;
	friend ostream& operator<<(ostream& o, const Box& b);
	friend istream& operator>>(istream& i, Box& b);
public:
	Box() {
		length = 1;
		width = 2;
		high = 3;
	}
};
ostream& operator<<(ostream& o,const Box& b) {
	o << b.length << ' ' << b.width << ' ' << b.high << endl;
	return o;
}
istream& operator>>(istream& i,Box& b) {
	i >> b.length;
	i >> b.width;
	i >> b.high;
	return i;
}
int main() {
	Box a, b,c;
	//cout << a;没有与这些操作数相匹配的运算符
	/*
	* 你想重载一个运算符要么在类内重载,要么在类外重载
	* 但是cout对象属于ostream类,该类我们无法修改,所以只能在类外用全局函数重载
	*/
    cin >> a>>b>>c;
    cout << a << b << c;
	return 0;
}

5.重载’++'运算符

#include <iostream>
using namespace std;
class A {
	//private 类内 
	int a;
	int b;
	int c;
	friend ostream& operator<<(ostream& o, const A& other);
public:
	A() {
		this->a = 0;
		this->b = 0;
		this->c = 0;
	}
	A& operator++() {//前加加
		a++;
		return *this;
	}
	A operator++(int) {//后加加
		//不要返回局部变量的引用
		A t = *this;
		a++;
		return t;
	}
};
//
ostream& operator<<(ostream& o, const A& other) {
	o << other.a << endl;
	return o;
}
int main() {
	A s;
	cout << s++;
	cout << ++s;
	return 0;
}

6.重载’比较’运算符

#include <iostream>
#include <iostream>
using namespace std;

class Person {
	string name;
	int age;
public:
	Person(string name, int age) {
		this->name = name;
		this->age = age;
	}
	bool operator==(const Person& other) const{
		if (this->age == other.age) {
			return 1;
		}
		return 0;
	}
	bool operator>(const Person& other) const{
		if (this->age > other.age) {
			return 1;
		}
		return 0;
	}
	bool operator<(const Person& other) const{
		if (this->age < other.age) {
			return 1;
		}
		return 0;
	}
	bool operator!=(const Person& other) const{
		if (this->age != other.age) {
			return 1;
		}
		return 0;
	}
};
int main() {
	Person p0("施易辰", 20);
	Person p1("田雪嵩", 100);
	if (p0 == p1) {
		cout << "年龄相等" << endl;
	}
	else if (p0 > p1) {
		cout << "p0大于p1" << endl;
	}
	else if (p0 < p1) {
		cout << "p0小于p1" << endl;
	}
	else
		cout << "p0不等于p1" << endl;
	return 0;
}

7.重载’='赋值运算符

编译器自动提供的赋值运算符是浅拷贝类型,如果成员变量指向堆区空间的话,会连续俩次析构函数会报错

#include <iostream>
#include <iostream>
using namespace std;

class Person {
	string name;
	int age;
	int* password;
	friend ostream& operator<<(ostream& o, const Person& p);
public:
	Person(string name, int age, int mm) {
		this->name = name;
		this->age = age;
		this->password = new int[10];
		*password = mm;
	}
	~Person() {
		if (password) delete[]password;
	}
};
ostream& operator<<(ostream& o, const Person& p) {
	o << p.name << ' ' << p.age << *(p.password) << endl;
	return o;
}
int main() {
	Person p0("施易辰", 20, 12345);
	Person p1("田雪嵩", 100, 123425);
	p0 = p1;//编译器自动提供的是浅拷贝类型的赋值运算符 成员变量指向堆区空间时候会报错
	cout << p0 << p1;
	return 0;
}
image-20231224131403075

所以如果成员变量指向堆区空间的话要自己重载赋值运算符,类型为深拷贝模式

#include <iostream>
#include <iostream>
using namespace std;

class Person {
	string name;
	int age;
	int* password;
	friend ostream& operator<<(ostream& o, const Person& p);
public:
	Person(string name, int age, int mm) {
		this->name = name;
		this->age = age;
		this->password = new int[10];
		*password = mm;
	}
	~Person() {
		if (password) delete[]password;
	}
	Person& operator=(const Person& other) {//深拷贝模式的赋值运算符
		this->name = other.name;
		this->age = other.age;
		this->password = new int[10];
		*password = *(other.password);
		return *this;
	}
};
ostream& operator<<(ostream& o, const Person& p) {
	o << p.name << ' ' << p.age << *(p.password) << endl;
	return o;
}
int main() {
	Person p0("施易辰", 20, 12345);
	Person p1("田雪嵩", 100, 123425);
	p0 = p1;//调用自定义的赋值运算符的重载函数
	cout << p0 << p1;
	return 0;
}
image-20231224131600567

不会报错!

8.重载’()'运算符

又称仿函数

#include <iostream>

using namespace std;

class Myprint {
public:
	void operator()(string text) {
		cout << text << endl;
	}
};
class Myadd {
public:
	int operator()(int a, int b) {
		return a + b;
	}
};
void text01() {
	Myprint myprint;
	myprint("abc");
	myprint.operator()("dce");
}
void text02() {
	int a = 1;
	int b = 2;
	Myadd myadd;
	cout << myadd.operator()(1, 2) << endl;
	cout << myadd(3, 4) << endl;
}
int main() {
	text01();
	text02();
	return 0;
}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值