09运算符重载

1、加号运算符重载

#include<iostream>
using namespace std;

//运算符重载
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//注意,只有自定义地数据类型可以发生运算符重载,内置的数据类型不能发生运算符重载

class Person
{
public:
	//1、成员函数重载+号
	//Person operator+(Person &p)
	//{
	//	Person tmp;
	//	tmp.m_A = this->m_A + p.m_A;
	//	tmp.m_B = this->m_B + p.m_B;
	//	return tmp;
	//}


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

//3运算符重载版本
Person operator+(Person& p1, int a)//为啥a不用引用呢?因为第61行的100是常量,不是变量,要是想传引用,得int a = 100;
{
	Person temp;
	temp.m_A = p1.m_A + a;
	temp.m_B = p1.m_B + a;
	return temp;
}
void test()
{
	Person p1, p2;
	p1.m_A = 10;
	p1.m_B = 20;
	p2.m_A = 10;
	p2.m_B = 20;

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

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


	Person p3 = p1 + p2;//上面两种方法的本质都可以简化成这种形式
	cout << p3.m_A << endl;
	cout << p3.m_B << endl;

	//运算符重载也可以发生函数重载
	
	Person p4 = p1 + 100;//注意!!!!100是常量,不用传地址
}
int main()
{
	test();

	return 0;
}

2、左移运算符重载

#include<iostream>
using namespace std;

//左移运算符重载
class Person
{

	friend ostream& operator<<(ostream& cout, Person& p);

public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
private:
	//利用成员函数重载左移运算符   p.operator<<(cout)  简化版本  p << cout 
	//不能利用成员函数重载<<运算符,因为无法实现cout 在左侧 接:18行
	/*Person operator<<( cout )
	{

	}*/
	int m_A;
	int m_B;
};

//只利用全局函数重载左移运算符
//ostream&   !!!!!链式编程思想,记得加&返回cout的地址
ostream& operator<<(ostream& cout, Person& p) //本质  operator<<(cout,p)  简化成cout << p;
{
	cout << p.m_A << p.m_B ;
	return cout;
}

void test()
{
	Person p(10, 10);
	//p.m_A = 10;
	//p.m_B = 10;

	cout << p << endl;//链式编程思想

}
int main()
{
	test();
	return 0;
}

3、递增运算符重载

#include<iostream>
using namespace std;

class Myint
{
	friend ostream& operator<<(ostream& cout, Myint myint);
public:
	Myint()
	{
		m_A = 0;
	}
	//重载前置++运算符
	Myint& operator++()
	{
		m_A++;
		return *this;
	}
	//重载后置++运算符
	//注意不能加&!!接24行
	Myint operator++(int)//注意!!!返回值类型不能实现函数重载,所以加int表示占位参数
	{
		Myint temp = *this;
		this->m_A++;
		return temp;//因为temp是局部变量,在成员函数执行完后会被释放,所以返回值类型不能加&!!!!这里不能实现链式思想
	}
private:
	int m_A;
};
//左移运算符重载
ostream& operator<<(ostream& cout, Myint myint)
{
	cout << myint.m_A;
	return cout;
}

//重载前置++运算符
void test()
{
	Myint myint;
	cout << ++myint << endl;
	cout << myint << endl;
}

//重载后置++运算符
void test02()
{
	Myint myint;
	cout << myint++ << endl;
	cout << myint << endl;
}
int main()
{
	test();
	test02();
	return 0;
}

4、递减运算符重载

#include<iostream>
using namespace std;

class Myint
{
	friend ostream& operator<<(ostream& cout, Myint myint);
public:
	Myint()
	{
		m_A = 0;
	}
	//重载前置--运算符
	Myint& operator--()
	{
		m_A--;
		return *this;
	}
	//重载后置--运算符
	Myint operator--(int) //int为占位参数
	{
		Myint temp = *this;
		this->m_A--;
		return temp;//返回的是局部变量,所以返回值类型不能加&
	}

private:
	int m_A;
};
//左移运算符重载
ostream& operator<<(ostream&cout,Myint myint)
{
	cout << myint.m_A;
	return cout;
}

//重载前置--运算符
void test01()
{
	Myint myint;
	cout << --myint << endl;
	cout << myint << endl;
}

//重载后置--运算符
void test02()
{
	Myint myint;
	cout << myint-- << endl;
	cout << myint << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}

5、赋值运算符

#include<iostream>
using namespace std;

class Person
{
public:
	Person(int age)
	{
		m_Age = new int(age);
	}
	~Person()
	{
		if (m_Age != NULL) //堆区的数据被重复释放,故程序会报错
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	//重载 赋值运算符
	Person& operator=(Person& p)
	{
		//判断自己给自己赋值的情况
		if (m_Age == p.m_Age)
		{
			return *this;

		}
		//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//深拷贝操作
		this->m_Age = new int(*p.m_Age);
		return *this;
	}
	int* m_Age;
};

void test()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);
	p3 = p2 = p1;
	//p3 = p3;//自己给自己赋值的情况接:22行
	cout << *p1.m_Age << endl;
	cout << *p2.m_Age << endl;
	cout << *p3.m_Age << endl;

}
int main()
{
	test();
	return 0;
}

6、关系运算符重载

#include<iostream>
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_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

void test()
{
	Person p1("Curry", 32);
	Person p2("Curry", 32);

	if (p1 == p2)
	{
		cout << "p1和p2是相等的" << endl;
	}
	else
	{
		cout << "p1和p2是不相等的" << endl;
	}
}

int main()
{
	test();
	return 0;
}

7、函数调用运算符重载

#include<iostream>
using namespace std;

class Myprint
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};

void test()
{
	Myprint myprint;
	cout << myprint.operator()(10, 20) << endl;
}
int main()
{
	test();


	return 0;
}

上一篇:09_多线程


下一篇:09 其他命令