C++学习第八十七篇

/*
* 运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
* 加号运算符重载
* 左移运算符重载
* 递增运算符重载
* 赋值运算符重载
* 关系运算符重载
* 函数调用运算符重载
* 注意:对于内置的数据类型表达式的运算符是不可能修改的(如1+1)
*       不要滥用运算符重载
*/
#include<iostream>
#include<string>
using namespace std;

class Person
{
public:
	Person() {};
	Person(int a,int b);
	~Person();
	//加号运算符重载:实现两个自定义数据类型相加的运算
	 //成员函数实现+号运算符重载
	Person operator+(const Person& p)
	{
		Person temp;//需要自行构造无参(默认)构造函数
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

	//左移运算符重载:可以输出自定义数据类型
	//成员函数实现不了 p<<cout 不是我们想要的结果
	//void operator<<(Person& p) {};
public:
	int m_A;
	int m_B;
};

Person::Person(int a,int b)
{
	this->m_A = a;
	this->m_B = b;
}

Person::~Person()
{
	cout << "Person析构函数" << endl;
}


//全局函数实现+号运算符重载
// 	Person operator+(const Person& p1, const Person& p2)
// 	{
// 		Person temp(0, 0);
// 		temp.m_A = p1.m_A + p2.m_A;
// 		temp.m_B = p1.m_B + p2.m_B;
// 		return temp;
// 	}

	//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int val)
{
	Person temp;
	temp.m_A = p2.m_A + val;
	temp.m_B = p2.m_B + val;
	return temp;
}

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p)
{
	out << "a = " << p.m_A << "b = " << p.m_B;
	return out;
}

void test()
{
	Person p1(10, 10);
	Person p2(20, 20);

	//成员函数方式
	Person p3 = p1 + p2;//相当于p2.operator+(p1);
	cout << "m_A = " << p3.m_A << "  m_B = " << p3.m_B << endl;

	Person p4 = p3 + 10;//相当于operator+(p3,10);
	cout << "m_A = " << p4.m_A << "  m_B = " << p4.m_B << endl;

	cout << p1 << " hello world" << endl;//左移重载,链式编程;如果没有进行左移重载,p1类型是无法直接输出的
}


//递增运算符重载
//作用:通过重载递增运算符,实现自己的递增操作
class MyInteger
{
	friend ostream& operator<<(ostream& out, MyInteger myint);
public:
	MyInteger()
	{
		m_Num = 0;
	 }
	//前置++
	MyInteger& operator++()
	{
		//先++
		m_Num++;
		//再返回
		return *this;
	}
	//后置++
	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();
	test();
	cout<<"Hello World!"<<endl;
	system("pause");
	return 0;
}
/*
* 赋值运算符重载
* c++编译器至少给一个类添加4个函数
* 1. 默认构造函数(无参,函数体为空)
* 2. 默认析构函数(无参,函数体为空)
* 3. 默认拷贝构造函数,对属性进行值拷贝
* 4. 赋值运算符operator=,对属性进行值拷贝
* 如果类中有属性指向堆区,做赋值运算操作时会出现深浅拷贝问题
*/
#include<iostream>
#include<string>
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 = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);

		//返回自身
		return *this;
	}


	~Person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}

	//年龄的指针
	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();

	//int a = 10;
	//int b = 20;
	//int c = 30;

	//c = b = a;
	//cout << "a = " << a << endl;
	//cout << "b = " << b << endl;
	//cout << "c = " << c << endl;

	system("pause");

	return 0;
}
/*
* 关系运算符重载:重载关系运算符,可以让两个自定义类型对象进行对比操作
*/

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

class Person 
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

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

	bool operator!=(Person& 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;
};

void test01()
{
	Person a("张三", 18);
	Person b("李四", 19);
	if (a==b)
	{
		cout << "a和b相等" << endl;
	} 
	else
	{
		cout << "a和b不相等" << endl;
	}
	if (a!=b)
	{
		cout << "a和b不相等" << endl;
	} 
	else
	{
		cout << "a和b相等" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}
/*
* 函数调用运算符重载
* 1、函数调用运算符即(),也可以重载
* 2、由于重载后使用的方式非常像函数的调用,因此称为仿函数
* 3、仿函数没有固定的写法,非常灵活
*/
#include<iostream>
#include<string>
using namespace std;

//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void test01()
{
	//重载的()操作符 使用起来非常类似函数调用 也称为仿函数
	MyPrint myprint;
	myprint("hello world");
}

//仿函数没有固定写法,非常灵活
//加法类
class  MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test02()
{
	MyAdd myadd;
	int ret = myadd(10, 10);
	cout << "ret = " << ret << endl;

	//匿名对象:类名();用完立即释放
	cout << "MyAdd()(20, 20) = " << MyAdd()(20, 20) << endl; 
}
int main()
{
	test01();
	test02();
	cout<<"Hello World!"<<endl;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值