各种重载的实现

@[TOC](加号运算符重载)

//class Person
//{
//public:
//
//	//1.成员函数重载 +  ,Person p3 = p1 + p2; 调用的本质是 Person p3 = p1.operator+(p2)
//	//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 p3 = p1 + p2;调用的本质Person p3 = operator+(p1,p2); 
//Person operator+(Person& p1,Person& p2)
//{
//	Person temp;
//	temp.m_a = p1.m_a + p1.m_a;
//	temp.m_b = p2.m_b + p2.m_b;
//	return temp;
//}
//
3.运算符重载  也可以发生函数重载 ,函数重载是指 函数名相同,传入的参数不同
//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 test()
//{
//	Person p1;
//	p1.m_a = 10;
//	p1.m_b = 20;
//	Person p2;
//	p2.m_a = 20;
//	p2.m_b = 30;
//
//	Person p3 = p1 + p2;
//	Person p4 = p1 + 100;
//
//	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()
//{
//	test();
//	return 0;
//}

@[TOC](左移运算符重载,左移运算符有就是cout,自定义输出)

//class Person
//{
//	friend ostream& operator<<(ostream& cout, Person& p);
//public:
//	//利用成员函数重载左移运算符, p.operator<<(cout)  === p<<cout 
//	//但是我们希望输出的结果是cout<<p,所以一般不用成员函数实现左移运算符的重载
//	// 一般用全局函数
//	//void operator<<()
//	Person(int a, int b)  //初始化
//	{
//		m_c = a;
//		m_d = b;
//	}
//
//private:
//	//利用成员函数重载左移运算符, p.operator<<(cout)  === p<<cout 
//	//但是我们希望输出的结果是cout<<p,所以一般不用成员函数实现左移运算符的重载
//	// 一般用全局函数
//	//void operator<<()
//	int m_c;
//	int m_d;
//};
//
 operator<<(ostream &cout,Person &p)简化版本是  cout <<p
void operator<<(ostream &cout,Person &p)
//
//ostream &operator<<(ostream& cout, Person& p)  //这么定义返回的任然是cout这个对象,就可以实现无线暑促
//{
//	cout << "m_c = " << p.m_c << " m_d =" << p.m_d <<endl;
//	return cout;
//}
//
//void test()
//{
//	Person p(10,10);
//
//	cout << p << "hello";  
//}
//
//int main()
//{
//	test();
//	return 0;
//}

@[TOC](递增运算符重载 ++)

//class MyInteger
//{
//	friend ostream& operator<<(ostream& cout, MyInteger& p);
//public:
//
//	MyInteger()  //初始化
//	{
//		m_num = 0;
//	}
//
//	//重载前置的++运算符,返回引用(MyInteger&)是为了一致对一个数据做操作
//	MyInteger& operator++()
//	{
//		m_num++;
//
//		//将自己返回,this是指向的自己,解引用返回本身
//		return *this;
//	}
//
//	//重载后置的++运算符,这里的int是一个占位参数,可以用于区分前置和后置递增
//	MyInteger operator++(int)
//	{
//		m_num++;
//
//		//将自己返回,this是指向的自己,解引用返回本身
//		return *this;
//	}
//
//private:
//	int m_num;
//};
//
//ostream &operator<<(ostream& cout, MyInteger& myint)  //这么定义返回的任然是cout这个对象,就可以实现无线暑促
//{
//	cout << myint.m_num;
//	return cout;
//}
//
//void test()
//{
//	MyInteger myint;
//
//	cout << ++(++myint);
//}
//
//void test02()
//{
//	MyInteger myint;
//
//	cout << ++(++myint);
//}
//
//int main()
//{
//	test();
//	return 0;
//}

@[TOC](赋值运算符重载)
//c++至少给一个类添加4个函数
//1.构造函数
//2.析构函数
//3.默认拷贝函数
//4.赋值运算符

//class Person
//{
//public:
//	Person(int age)
//	{
//		m_age = new int(age);  //new + 数据类型,是构造该数据类型的指针
//	}
//
//	//析构,释放内存,堆区的数据(指针),手动开辟的空间,需要手动进行释放
//	~Person()
//	{
//		if (m_age != NULL)
//		{
//			delete m_age;
//			m_age = NULL;
//		}
//	}
//
//	//赋值运算符重载  ,编译器提供的是浅拷贝
//	Person& operator = (Person &p1)
//	{
//		//编译器提供的是浅拷贝,m_age = p.m_age,指针和指针之间之间赋值
//
//
//		//传进来的是p1,想要将p1的属性全部拷贝给p2,但是P2本身就有一个内存空间,
//		//所以应该先将P2的内存空间释放干净
//		if (m_age != NULL)
//		{
//			delete m_age;
//			m_age = NULL;
//		}
//
//		//进行深拷贝的操作
//		m_age = new int(*p1.m_age);
//		return *this;
//	}
//	int* m_age;
//};
//
//void test()
//{
//	Person p1(18);
//	
//	Person p2(20);
//	Person p3(30);
//
//	//这里会崩溃,因为这里是浅拷贝,上面手动开辟了数据存储的空间且空间在堆区(指针),
//	// 就手动进行释放,
//	// 浅拷贝会将p1的地址内存完全不变的拷贝到p2,析构函数在类的调用结束之后,是会自动调用
//	// 来释放类里面的数据内存的。由于P1,P2的地址是一样的,类对同一块内存释放了两次,所以报错
//	p3 = p2 = p1;
//	cout << "p1age is =" << *p1.m_age << endl;
//	cout << "p2age is =" << *p2.m_age << endl;
//	cout << "p3age is =" << *p3.m_age << endl;
//}
//
//int main()
//{
//	test();
//
//	int a = 10, b = 20, c = 30;
//	c = b = a;
//	cout << "a = " << a << endl;
//	cout << "b = " << b << endl;
//	cout << "c = " << c << endl;
//	return 0;
//}

@[TOC](关系运算符重载,大于小于等于等)

//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;
//	}
//
//	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 test()
//{
//	Person p1("wang", 18);
//	Person p2("dang", 18);
//
//	if (p1 == p2)
//	{
//		cout << "same" << endl;
//	}
//
//	if (p1 != p2)
//	{
//		cout << "not same" << endl;
//	}
//}
//
//int main()
//{
//	test();
//	return 0;
//}



@[TOC](函数调用运算()符重载)
//由于重载后的使用方式非常像函数的调用,因此称为防函数,仿函数没有固定的写法,非常灵活

class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};

void MyPrint02(string test)
{
	cout << test << endl;
}

void test()
{
	MyPrint myprint;
	myprint("hello");  //使用起来非常像仿函数

	MyPrint02("hello");
}


//仿函数非常灵活,没有固定的写法
//加法
class MyAdd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};

void test2()
{
	MyAdd add;
	cout << add(1, 2) << endl;

	//匿名函数对象,当前行执行结束立即释放,上面两行语句给对象一个名字,叫add
	cout << MyAdd()(100, 100) << endl;
}

int main()
{
	test();
	test2();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值