C++运算符重载

运算符重载:可以对这些运算符重新定义,让他有新的功能。适应不同的数据类型。

1.加号

可以用成员函数重载和全局函数重载

重要的是 operator ;

直接看例子:

#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 = 10;
	Person p2;
	p2.m_a = 10;
	p2.m_b = 10;


	//成员函数本质形式
	// p3 = p1.operator+(p2);

	//全局函数本质形式
	//p3 = operator+(p1,p2);

	//成员函数与全局函数的简化形式都是这样
	Person p3 = p1 + p2;

	cout << "p3.m_a = " << p3.m_a << endl;
	cout << "p3.m_b = " << p3.m_b << endl;

	//运算符重载也可以函数重载
	Person p4 = p1 + 100;

	cout << "p4.m_a = " << p4.m_a << endl;
	cout << "p4.m_b = " << p4.m_b << endl;

}

int main()
{
	test01();

	return 0;
}

 注意:1.int,double 等内置的数据类型的表达式中运算符不会被影响。

2.不能滥用重载,比如名字是  operator+,结果函数里面写的是减法。

2.左移运算符

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

#include<iostream>
using namespace std;


class Person
{

public:
	

	int m_a;
	int m_b;
};


//注意:只能用全局函数写;若用成员函数,就必须是 p<<cout;


/*这样的可以,但在test01()中 cout << p; 后面不能加endl, 而且一次只能输出一个对象

void operator<<(ostream &cout,Person &p)
{
	cout << p.m_a << " " << p.m_b << endl;
}
*/

ostream& operator<<(ostream& cout, Person& p)//ostream 是cout 的类,标准输出流
{
	cout << p.m_a << " " << p.m_b << endl;
	return cout;
}

void test01()
{
	Person p;
	p.m_a = 10;
    p.m_b = 20;
	cout << p << endl;
}

int main()
{
	test01();


	return 0;
}

3.递增运算符

#include<iostream>
using namespace std;

class myinteger
{
	friend ostream& operator<<(ostream& cout, myinteger myint);
public:

	myinteger()
	{
		m_num = 0;
	}

	//重载 前置 ++ 运算符
	//返回引用是要一直对一个数据进行递增运算
	myinteger& operator++() 
	{
		m_num++;//先++ 运算
		return *this;//再将自身返回
	}
	//重载 后置 ++ 运算符
	//int 代表占位参数,用于区分前置和后置递增
	//这里返回值,没返回引用,因为返回的是局部变量
	myinteger operator++(int)
	{
		//先记录当时结果
		myinteger temp = *this;
		//后递增
		m_num++;
		//最后将记录返回
		return temp;
	}

private:
	int m_num;
};

//重载<<运算符
ostream& operator<<(ostream& cout, myinteger myint)
{
	cout << myint.m_num;

	return cout;
}

void test01()
{
	myinteger myint;
	cout << ++myint << endl;
}

void test02()
{
	myinteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main()
{
	test01();
	test02();

	return 0;
}

4.重载赋值运算符

operator=  对属性进行值拷贝

#include<iostream>
using namespace std;


class Person 
{
public:

	Person(int age)
	{
		//在堆区开辟年龄数据
		m_age = new int(age);
	}

	Person& operator=(Person& p)
	{
		//判断指针是否为空
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
		//深拷贝
		m_age = new int(*p.m_age);
		//返回自身
		return *this;

	}

	//年龄数据,要用new在堆区开辟,new会返回该数据类型的指针类型,所以用指针
	int* m_age;
};

void test01()
{
	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()
{
	test01();

	return 0;
}

5.关系运算符 

 让两个自定义类型的数据进行比较

#include<iostream>
using namespace std;


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

	string m_name;
	int m_age;
};

void test01()
{
	Person p1("zhangsan", 18);
	Person p2("zhangsan", 18);
    //验证一下自己写的重载对不对
	if (p1 == p2)
	{
		cout << "p1与p2相同" << endl;
	}
	else {
		cout << "p1与p2不相同" << endl;
	}
}

int main()
{
	test01();

	return 0;
}

6.函数调用运算符重载

函数调用运算符 “()”也可以进行重载

由于重载后使用时的形式非常像函数的调用,又叫仿函数 

仿函数没有固定写法,非常灵活

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


class MyPrintf
{
public:
	void operator()(string test)//重载函数调用运算符,打印输出
	{
		cout << test << endl;
	}
};

//正常打印输出函数
void MYprintf(string test)
{
	cout << test << endl;
}


void test01()
{
	MYprintf("hello world");//正常调用函数

	MyPrintf myprintf;//用重载函数调用运算符
	myprintf("hello world");//由于重载后使用时的形式非常像函数的调用,又叫仿函数 

}


//仿函数没有固定写法,非常灵活
class MyAdd//比如还可以写个加法的
{
public:

	int operator()(int a, int b)
	{
		return a + b;
	}

};

int MYadd(int a, int b)
{
	return a + b;
}

void test02()
{
	int res1=MYadd(10, 20);

	MyAdd myadd;
	int res2 = myadd(10, 10);
	cout << res1 << endl;
	cout << res2 << endl;

}
int main()
{
	test01();
	test02();
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值