[c++学习笔记09]:运算符重载

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

一、加号运算符重载
//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;
}
//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;
}
//成员函数的本质调用:
//Person p3 = p1.operator+(p2);
//全局函数的本质调用:
Person p3 = operator+(p1, p2);

#include<iostream>
using namespace std;
#include<string>
//加号运算符重载
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 = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	//成员函数的本质调用:
	//Person p3 = p1.operator+(p2);
	//Person p3 = p1 + p2;
	//全局函数的本质调用:
	Person p3 = operator+(p1, p2);
	//运算符重载也可以发生函数重载
	Person p4 = p1 + 12;
	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;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

注意:
1、对于内置的数据类型的表达式的的运算符是不可能改变的
2、不要滥用运算符重载

————————————————————————————————————————

二、 左移运算符重载
作用:可以输出自定义数据类型
cout的类型是ostream
不能用成员函数重载,只能用全局函数重载。

ostream &operator<<(ostream& out, Person& p) //otream是cout 的类型:输出流对象
{
out << “p.m_A " << p.m_A << " p.m_B” << p.m_B;
return out;
}

//实现了一下输出了p的所有信息

#include<iostream>
using namespace std;
#include<string>
//左移运算符重载
class Person
{
	friend ostream& operator<<(ostream& out, Person& p);
	//成员函数重载<<运算符     p.operator(p)   简化:p<<cout
	//不能利用成员函数重载<<运算符,因为无法实现cout 在左侧
	//void operator<<(cout)  
	//{}
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) //otream是cout 的类型:输出流对象
{
	out << "p.m_A " << p.m_A << " p.m_B" << p.m_B;
	return out;
}

void test01()
{
	Person p(10, 10);
	cout << p << "你好" << endl;//实现了一下输出了p的所有信息
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

三、递增运算符重载

作用: 通过重载递增运算符,实现自己的整型数据
//重载前置++运算符 返回引用是为了一直对一个数据进行递增操作
MyInteger& operator++()
{
//先++
m_Num++;
//再返回
return *this;
}

//重载后置++运算符
//MyInteger operator++(int)  int 代表占位参数,可以用于区分前置递增和后置递增
MyInteger operator++(int) {
	//先返回
	MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
	m_Num++;
	return temp;
}
#include<iostream>
using namespace std;
#include<string>
//重载递增运算符
//自定义类型
class MyInteger {

	friend ostream& operator<<(ostream& out, MyInteger myint);

public:
	MyInteger() {
		m_Num = 0;
	}
	//重载前置++运算符  返回引用是为了一直对一个数据进行递增操作
	MyInteger& operator++() 
	{
		//先++
		m_Num++;
		//再返回
		return *this;
	}

	//重载后置++运算符
	//MyInteger operator++(int)  int 代表占位参数,可以用于区分前置递增和后置递增
	MyInteger operator++(int) {
		//先返回
		MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
		m_Num++;
		return temp;
	}

private:
	int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_Num;
	return out;
}


//前置++ 先++ 再返回
void test01() {
	MyInteger myInt;
	cout << ++myInt << endl;
	cout << myInt << endl;
}

//后置++ 先返回 再++
void test02() {

	MyInteger myInt;
	cout << myInt++ << endl;
	cout << myInt << endl;
}

int main() {

	//test01();
	test02();

	system("pause");

	return 0;
}

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

四、赋值运算符重载

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

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

#include<iostream>
using namespace std;
#include<string>
//赋值运算符重载
class Person
{
public:
	Person(int a)
	{
		m_Age = new int(a);
	}
	~Person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	Person& operator=(Person& p)
	{
		//编译器提供的是浅拷贝
		//m_Age=a;
		//先判断是否有属性在堆区,如有,先释放干净再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//深拷贝
		m_Age = new int(*p.m_Age);
		return *this;
	}
	int *m_Age;
};

void test01() 
{
	Person p1(10);
	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()
{
	test01();
	system("pause");
	return 0;
}


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

#include<iostream>
using namespace std;
#include<string>
//关系运算符重载
class Person
{
public:

	Person(string a,int b)
	{
		this->name = a;
		this->age = b;
	}
	bool operator==(Person& p)
	{
		if (this->age == p.age && this->name == p.name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator!=(Person& p)
	{
		if (this->age == p.age && this->name == p.name)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

private:
	string name;
	int age;
};

void test01()
{
	Person p1("刘备", 19);
	Person p2("刘3备",19);
	if (p1 == p2)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout<<"不相等"<<endl;
	}
	if (p1 != p2)
	{
		cout << "部相等" << endl;
	}
	else
	{
		cout << "相=等" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

六、函数调用运算符重载

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

void test01() 
{
	//重载的()操作符 也称为仿函数
	Person p;
	p("hello !");

}
class Myadd
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};
void test02() 
{
	Myadd a;
	int b=a(1, 4);
	cout << "b = " << b << endl;

	//匿名对象的调用,不用创建类,直接调用函数
	cout << "Myadd()(100,100) = " << Myadd()(100, 100) << endl;
}

int main() {

	//test01();
	test02();

	system("pause");

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值