C++学习笔记17-类和对象-运算符重载

17.0 前言

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

返回类型  operator<运算符符号>(<参数>){}
//operator<运算符符号>为: operator+,operator-,operator<<, ...

17.1 加号运算符重载

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

#include<iostream>
using namespace std;

//加号运算符重载

class Person
{
public:
	//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;
	//}

	int m_A;
	int m_B;
};
//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()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 20;
	Person p2;
	p2.m_A = 50;
	p2.m_B = 60;

	Person p3 = p1 + p2;
	//Person p4 = p1.operator+(p2);
	Person p4 = operator+(p1, p2);
	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;
	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl;

	Person p5 = p1 + 10;//Person + int
	cout << "p5.m_A = " << p5.m_A << endl;
	cout << "p5.m_B = " << p5.m_B << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

注意:

  • 对于内置的数据类型表达式的运算符是不可以改变的,例如1+1=2,不能手动修改为1+1=0 or 其他。 只能赋予它新的运算。

  • 不要滥用运算符重载,别瞎写。

17.2 左移运算符重载

作用:可以输出自定义数据类型。
语法:

ostream& operator<<(ostream& cout, 数据类型 对象)
或者
ostream& operator<<(ostream& cout, 数据类型& 对象)

这两个根据情况选择使用,传入的ostream参数必须是引用,因为ostream这个类里面的拷贝构造函数是private的。


示例:

#include<iostream>
using namespace std;

class Person2
{
	friend ostream& operator<<(ostream& out, Person2& p);
public:
	Person2(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	//利用成员函数重载左移运算符,本质调用为:p.operator<<(cout),即 p<<cout,无法实现cout在左侧。
	//所以一般不会用成员函数重载<<运算符。
	//void operator<<(cout)
	//{}
private:
	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符,本质调用为operator<<(cout ,p),即cout << p。
ostream& operator<<(ostream& cout, Person2& p)  //cout属于ostream这个类,这里cout只是引用名称,可以更换为任意名字
{
	cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
	return cout;
}
void test2_01()
{
	Person2 p(10,20);
	operator<<(cout, p) << endl; //使用endl是编译器自带的左移运算符功能,所以后面必须用默认的写法。
	cout << p << endl;
	cout << p << " hello" << endl;//链式编程,使函数返回类型为本身,从而可以继续调用此函数。
}
int main()
{
	test2_01();
	system("pause");
	return 0;
}

17.3 递增运算符重载

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

operator++() 为前置递增。
operator++(int) 为后置递增(int代表占位参数,写了它以后编译器就认为是后置递增)。

  • 因为后置递增返回的是原先对象的值,并且原对象还需要加1,所以不能返回其引用类型。
  • 因此如果要使用左移运算符的重载,那么传入参数的数据类型也不能使用引用类型
  • 同时也要注意自己定义的类中的拷贝构造函数权限不能是private和protected。

示例:

#include<iostream>
using namespace std;

class Myinterger
{
	friend ostream& operator<<(ostream& cout, Myinterger myint);
public:
	Myinterger()
	{
		m_Num = 0;
	}

	//前置运算符
	Myinterger& operator++()  //返回引用是为了用原对象,而不是新对象
	{
		m_Num++;
		return *this;
	}
	//后置运算符,int代表占位参数,写了它以后编译器就认为是后置递增
	Myinterger operator++(int)   //因为这是在局部定义了一个临时的temp,所以只能返回值,不能返回引用或者指针.
	{
		Myinterger temp = *this;
		m_Num++;
		return temp;
	}
private:
	int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& cout, Myinterger myint)   //后置运算符返回的是Myinterger型而不是它的引用型,所以不可以用引用类型。
{
	cout << "m_Num = " << myint.m_Num << endl;
	return cout;
}
void test3_01()
{
	Myinterger myint;
	cout << ++(++myint) << endl;
}
void test3_02()
{
	Myinterger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main()
{
	test3_01();
	test3_02();
	system("pause");
	return 0;
}

17.4 赋值运算符重载

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

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

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

示例:

#include<iostream>
using namespace std;

class Person4
{
public:
	Person4(int age)
	{
		m_Age = new int(age);
	}
	~Person4()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	//重载 赋值运算符
	Person4& operator=(Person4& p) 
	{
		//编译器是浅拷贝
		//我们需要深拷贝

		//先判断是否有属性在堆区,如果有,先释放干净,然后再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);
		
		return *this;
	}

	int* m_Age;
};

void test4_01()
{
	Person4 p1(18);
	Person4 p2(20);
	Person4 p3(10);
	p3 = p2 = p1;   //为了可以连续赋值,需要返回对象自身。
	cout << "p1的年龄是: " << *p1.m_Age << endl;
	cout << "p2的年龄是: " << *p2.m_Age << endl;
	cout << "p3的年龄是: " << *p3.m_Age << endl;
}
void test4_02()
{
	int a = 10;
	int b = 20;
	int c = 30;
	c = b = a;   //默认赋值操作符的是支持连续赋值的
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
}

int main()
{	
	test4_01();
	test4_02();
	system("pause");
	return 0;
}

17.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作。
有成员函数和全局函数两种。

示例:

#include<iostream>
using namespace std;
class Person5
{
public:
	Person5(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}

	重载关系运算符,局部函数
	//bool operator==(Person5& p)
	//{
	//	if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
	//		return true;
	//	else { return false; }
	//}

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

	string m_Name;
	int m_Age;
};

bool operator==(Person5& p1,Person5& p2)  //全局函数
{
	if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name)
		return true;
	else { return false; }
}
void test5()
{
	Person5 p1("Tom", 18);
	Person5 p2("Tom", 20);
	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()
{ 
	test5();
	system("pause");
	return 0;
}

17.6 函数调用运算符重载

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

class Myprint
{
public:

	void operator()(string test)
	{
		cout << test << endl;
	}
};
void myprint2(string test)
{
	cout << test << endl;
}

void test6()
{
	Myprint myprint;
	myprint("hello world"); //使用起来非常类似于函数调用,因此成为仿函数
	myprint2("hello world");
}

//仿函数非常灵活。
//加法类
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};
void test6_02()
{
	MyAdd add;
	cout << add(1, 3) << endl;
	//匿名函数对象 使用语法:类名()
	cout << MyAdd()(1, 3) << endl;
}

int main()
{
	test6();
	test6_02();
	system("pause");
	return 0;
}

注:
匿名函数对象与构造函数的调用方式显示法相关。
使用语法为:

类名(参数)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值