C++#8(运算符重载)

 

目录

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

一、加号运算符重载+

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

二、 左移运算符重载<<

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

  解惑时间:

 三、递增运算符重载++

作用:通过重弄在递增运算符,实现增加的整形相加

解惑时间: 

四、 赋值运算符重载=

作用:解决类中有属性在堆区,使用赋值操作时出现的深浅拷贝问题

解惑时间:

 五、关系运算符重载==/!=

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

 六、重载函数调用运算符()

由于使用方法类似于函数的调用,因此也被称为仿函数


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

一、加号运算符重载+

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

(1)使用成员函数实现+号的重载 

operator+是编译器给的通用名称,使用时可以帮忙简化

(2)使用全局函数实现+号的重载

 (3)运算符重载也可以实现函数重载

二、 左移运算符重载<<

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

#include<iostream>
using namespace std;
//左移运算符重载
class Person
{
	//友元的使用 friend
	friend ostream& operator<<(ostream& cout, Person& p);
public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
private:
	int m_A;
	int m_B;

};
//使用全局函数实现<<的重载
ostream& operator<<(ostream& cout, Person& p)
{
	cout << "p.m_A = " << p.m_A << "p.m_B = " << p.m_B;
	return cout;
}

void test1()
{
	Person p1(10, 20);
	cout << p1 << endl;

}
int main()
{
	test1();

	system("pause");

	return 0;
}

  解惑时间:

1.为什么没有使用成员函数的形式实现<<的重载?

因为在实现<<的重载时,cout 必须是左值,而在通过成员函数去实现<<的重载的时候,

cout只能出现在右值位置,所以无法实现

2.ostream是什么?

ostream是标准输出流,cout的类型就是标准输出流

3.为什么全局函数返回类型是ostream&?

返回ostream& 就是返回标准输出流的意思,这样在使用<<时就可以继续往后增加内容

 三、递增运算符重载++

作用:通过重弄在递增运算符,实现增加的整形相加

#include<iostream>
using namespace std;

//++的重载
class MyInteger
{
	friend void test1();
public:
	MyInteger()
	{
		m_A = 0;
	}
	//成员函数前置++的实现
	MyInteger& operator++()
	{
		//先++
		m_A++;
		//再返回
		return *this;
	}
	//成员函数后置++的实现
	MyInteger operator++(int)
	{
		//先存储
		MyInteger num = *this;
		//然后++
		m_A++;
		//最后再返回没增加前的对象
		return num;
	}
private:
	int m_A;
};
void test1()
{
	MyInteger p;
	p.m_A = 1;

	cout << ++(++p.m_A) << endl;
	cout << p.m_A++ << endl;
	cout << p.m_A << endl;
}
int main()
{
	test1();

	system("pause");
	return 0;
}

解惑时间: 

 (1)MyInteger operator++(int)之中的int是什么?

 MyInteger operator++(int)之中的int是占位参数,是用来区分前置递增和后置递增的

递减运算符

四、 赋值运算符重载=

作用:解决类中有属性在堆区,使用赋值操作时出现的深浅拷贝问题

#include<iostream>
using namespace std;

class Person
{
	friend void test1();
public:
	Person(int age)
	{
		m_age = new int(age);
	}
	~Person()
	{
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
	}
	Person& operator=(Person &p)
	{
		//编译提供的浅拷贝
		//m_age = p.m_age;

		//先判断是不是空指针
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
		m_age = new int(*p.m_age);

		return *this;
	}

private:
	int* m_age;

};
void test1()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);

	p3 = p2 = p1;

	cout << "p1的年龄是:" << *p1.m_age << endl;
	cout << "p2的年龄是:" << *p2.m_age << endl;
	cout << "p3的年龄是:" << *p3.m_age << endl;

}
int main()
{
	test1();

	system("pause");
	return 0;
}

 深拷贝与浅拷贝可以看一下这篇 http://t.csdn.cn/hUlsK

解惑时间:

(1)为什么成员函数的返回类型是Person&?

因为只有返回对象的别名才能进行连续赋值的操作

(2)return *this 是返回这个对象本身

 五、关系运算符重载==/!=

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

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

class Person
{
public:
	Person(string name,int age)
	{
		m_name = name;
		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()
{
	Person p1("怪物", 3);
	Person p2("怪", 3);

	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;
}

解惑时间:

好像没有什么难的点,就不解惑了

 六、重载函数调用运算符()

由于使用方法类似于函数的调用,因此也被称为仿函数

匿名函数对象(使用后被立即回收)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值