c/c++入门教程 - 2.4.5 c++运算符重载(加号、左移、递增、赋值、关系、函数调用)

目录

4.5 运算符重载

4.5.1 加号运算符重载

4.5.2 左移运算符重载

4.5.3 递增运算符重载

4.5.4 赋值运算符重载

4.5.5 关系运算符重载

4.5.6 函数调用运算符重载

相关教程


4.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

 

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加的运算。

operator+ 的简化写法就是 +。

  • 有三种重载方法:

1.成员函数重载+号,Person operator+(Person& p)

2.全局函数重载+号,Person operator+(Person& p1, Person& p2)

3.函数重载的版本,Person operator+(int num),Person operator+(Person& p1, int num)

  • 总结:

对于内置的数据类型的表达式的运算符是不可能改变的。

不要滥用运算符重载。

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

class Person
{
public:

	int m_A;
	int m_B;

	// 通过自己写成员函数,实现两个对象相加属性后返回新的对象
	Person PersonAddPerson(Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

	// 编译器给起了一个通用的名称 operator+
	// 1.成员函数重载+号
	Person operator+(Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

	 函数重载的版本
	//Person operator+(int num)
	//{
	//	Person temp;
	//	temp.m_A = this->m_A + num;
	//	temp.m_B = this->m_B + num;
	//	return temp;
	//}
};

 2.全局函数重载+号
//Person operator+(Person& p1, Person& p2)
//{
//	Person temp;
//	temp.m_A = p1.m_A + p2.m_A;
//	temp.m_B = p1.m_B + p2.m_B;
//	return temp;
//}

// 函数重载的版本
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}


void test01 (void)
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 20;

	Person p2;
	p2.m_A = 30;
	p2.m_B = 40;

	Person p3 = p1.PersonAddPerson(p2);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl << endl;

	// 成员函数重载本质调用
	Person p4 = p1.operator+(p2);

	// 全局函数重载本质调用
	//Person p4 = operator+(p1, p2);

	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl << endl;

	// 简化形式
	Person p5 = p1 + p2;
	cout << "p5.m_A = " << p5.m_A << endl;
	cout << "p5.m_B = " << p5.m_B << endl << endl;

	// 运算符重载,也可以发生函数重载
	Person p6 = p1 + 100;	// Person + int
	cout << "p6.m_A = " << p6.m_A << endl;
	cout << "p6.m_B = " << p6.m_B << endl << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
-----------------------------------------------------------------------
p3.m_A = 40
p3.m_B = 60

p4.m_A = 40
p4.m_B = 60

p5.m_A = 40
p5.m_B = 60

p6.m_A = 110
p6.m_B = 120

请按任意键继续. . .

 

 

4.5.2 左移运算符重载

作用:可以输出自定义的类型。

 

形如 cout << a << endl;

利用成员函数  左移运算符  p.operator<<(cout)  简化版本  p << cout

不能利用成员函数重载<<运算符,因为无法实现 cout在左侧。

如果要达成 cout << 对象,由于类中的成员函数调用,对象只能在左侧,所以只能利用全局函数重载左移运算符。

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

class Person
{
	friend ostream& operator<<(ostream& out, Person& p);

public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
private:

	int m_A;
	int m_B;
};

// 只能利用全局函数重载左移运算符
ostream& operator<<(ostream& out, Person& p)
{
	out << "m_A = " << p.m_A << "  m_B = " << p.m_B;
	return out;
}

void test01 (void)
{
	Person p1(10, 20);
	Person p2(30, 40);

	cout << p1 << endl << p2 << "Hello world!" << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
--------------------------------------------------------------------
m_A = 10  m_B = 20
m_A = 30  m_B = 40Hello world!
请按任意键继续. . .

 

 

4.5.3 递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据。

重载前置++运算符、后置++运算符。

总结:前置递增返回的是引用,后置递增返回的是值。

 

注意:ostream& operator<<(ostream& out, Person& p);

ostream& operator<<(ostream& out, Person p);他俩cout的输入参数不一样,用错了会报错。

 

前置递增重载:

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

class Person
{
	friend ostream& operator<<(ostream& out, Person& p);
	//friend ostream& operator<<(ostream& out, Person p);

public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}

	// 重载前置++运算符  返回引用为了一直对一个数据进行递增操作
	Person& operator++()
	{
		m_A++;
		return *this;
	}

	// 重载后置++运算符
	Person operator++(int)	// int代表占位参数,可以用于区分前置和后置递增
	{
		Person temp = *this;
		m_A++;
		return temp;
	}

private:

	int m_A;
	int m_B;
};

// 只能利用全局函数重载左移运算符
ostream& operator<<(ostream& out, Person& p)
{
	out << "m_A = " << p.m_A << "  m_B = " << p.m_B;
	return out;
}

//ostream& operator<<(ostream& out, Person p)
//{
//	out << "m_A = " << p.m_A << "  m_B = " << p.m_B;
//	return out;
//}

void test01 (void)
{
	Person p1(10, 20);
	Person p2(30, 40);

	cout << ++p1 << endl << p2 << "Hello world!" << endl;
	cout << p1 << endl << endl;
}

//void test02()
//{
//	Person p1(10, 20);
//	cout << p1++ << endl;
//	cout << p1 << endl << endl;
//}

int main() {

	test01();
	//test02();

	system("pause");
	return 0;
}
---------------------------------------------------------------------------
m_A = 11  m_B = 20
m_A = 30  m_B = 40Hello world!
m_A = 11  m_B = 20

请按任意键继续. . .

 

后置递增的重载:

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

class Person
{
	//friend ostream& operator<<(ostream& out, Person& p);
	friend ostream& operator<<(ostream& out, Person p);

public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}

	// 重载前置++运算符  返回引用为了一直对一个数据进行递增操作
	Person& operator++()
	{
		m_A++;
		return *this;
	}

	// 重载后置++运算符
	Person operator++(int)	// int代表占位参数,可以用于区分前置和后置递增
	{
		Person temp = *this;
		m_A++;
		return temp;
	}

private:

	int m_A;
	int m_B;
};

 只能利用全局函数重载左移运算符
//ostream& operator<<(ostream& out, Person& p)
//{
//	out << "m_A = " << p.m_A << "  m_B = " << p.m_B;
//	return out;
//}

ostream& operator<<(ostream& out, Person p)
{
	out << "m_A = " << p.m_A << "  m_B = " << p.m_B;
	return out;
}

void test01 (void)
{
	Person p1(10, 20);
	Person p2(30, 40);

	cout << ++p1 << endl << p2 << "Hello world!" << endl;
	cout << p1 << endl << endl;
}

void test02()
{
	Person p1(10, 20);
	cout << p1++ << endl;
	cout << p1 << endl << endl;
}

int main() {

	test01();
	test02();

	system("pause");
	return 0;
}
-----------------------------------------------------------------------
m_A = 11  m_B = 20
m_A = 30  m_B = 40Hello world!
m_A = 11  m_B = 20

m_A = 10  m_B = 20
m_A = 11  m_B = 20

请按任意键继续. . .

 

4.5.4 赋值运算符重载

C++编译器至少给一个类添加4个函数:

  • 1. 默认构造函数(无参,函数体为空)
  • 2. 默认析构函数(无参,函数体为空)
  • 3. 默认拷贝构造函数,对属性进行值拷贝
  • 4. 赋值运算符 operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题。

 

注意:加了析构,释放堆区内存,但如果没有重载赋值运算符,程序会崩溃

return *this; 是为了可以形成 p1 = p2 = p3 链式结构。

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

class Person
{

public:
	Person(int a)
	{
		m_A = new int(a);
	}
	
	~Person()
	{
		if (m_A != NULL)
		{
			delete m_A;
			m_A = NULL;
		}
	}

	// 重载赋值运算符
	Person& operator=(Person &p)
	{
		// 编译器提供的是浅拷贝
		// m_A = p.m_A;

		// 先判断释放有属性在堆内存,如果有先释放干净,然后再深拷贝
		if (m_A != NULL)
		{
			delete m_A;
			m_A = NULL;
		}

		// 深拷贝
		m_A = new int(*p.m_A);

		// 返回对象本身
		return *this;
	}

	int *m_A;
};

void test01 (void)
{
	Person p1(10);
	Person p2(20);
	p2 = p1;

	Person p3(18);
	p1 = p2 = p3;

	cout << *p1.m_A << endl;
	cout << *p2.m_A << endl;
	cout << *p3.m_A << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
---------------------------------------------------------------------
18
18
18
请按任意键继续. . .

 

4.5.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

示例:

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

class Person
{

public:
	Person(string str, int age)
	{
		m_Name = str;
		m_Age = age;
	}

	bool operator==(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return true;
		else return false;
	}

	bool operator!=(Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
			return false;
		else return true;
	}

	string m_Name;
	int m_Age;
};

void test01 (void)
{
	Person p1("XiaoMing", 18);
	Person p2("XiaoMing", 18);

	if (p1 == p2)
		cout << "p1 p2 相等" << endl;
	else cout << "p1 p2 不等" << endl;

	if (p1 != p2)
		cout << "p1 p2 不等" << endl;
	else cout << "p1 p2 相等" << endl;

}

int main() {

	test01();

	system("pause");
	return 0;
}
---------------------------------------------------------------------------
p1 p2 相等
p1 p2 相等
请按任意键继续. . .

 

4.5.6 函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include <iostream>
using namespace std;
#include <string>

class MyPrint
{

public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};

void test01 (void)
{
	MyPrint myPrint;
	myPrint("Hello World!");
}

class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd myAdd;
	int ret = myAdd(100, 100);
	cout << "ret = " << ret << endl;

	// 匿名对象调用
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

	test01();
	test02();

	system("pause");
	return 0;
}
----------------------------------------------------------------------------
Hello World!
ret = 200
MyAdd()(100,100) = 200
请按任意键继续. . .

 

 

相关教程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值