瀏覽代碼

上传文件至 '运算符重载2'

Creamo 2 年之前
父節點
當前提交
f94701ff42

+ 101 - 0
运算符重载2/4、赋值运算符重载.cpp

@@ -0,0 +1,101 @@
+//4.5.4   赋值运算符重载
+
+//C++编译器至少给一个类添加4个函数
+
+//1、默认构造函数(无参,函数体为空)
+
+//2、默认析构函数(无参,函数体为空)
+
+//3、默认拷贝构造函数,对属性进行值拷贝
+
+//4、赋值运算符operator= , 对属性进行值拷贝
+
+//如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
+
+#if(0)
+
+#include <iostream>
+
+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)
+	{
+		//编译器是提供的浅拷贝
+
+		//m_Age = p.m_Age;
+
+		//应该先判断是否有属性在堆区,如果有先释放干净,再深拷贝
+		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(18);
+
+	Person p2(20);
+
+	Person p3(30);
+
+	p3 = p2 = p1;                    //赋值运算操作
+
+	std::cout << "p1的年龄为:" << *p1.m_Age << std::endl;
+	std::cout << "p2的年龄为:" << *p2.m_Age << std::endl;
+	std::cout << "p3的年龄为:" << *p3.m_Age << std::endl;
+}
+
+int main()
+{
+	test01();
+
+	//int a = 10;
+
+	//int b = 20;
+
+	//int c = 30;
+
+	//c = b = a;
+
+	//std::cout << "a = " << a << std::endl;
+	//std::cout << "b = " << b << std::endl;
+	//std::cout << "c = " << c << std::endl;
+
+	system("pause");
+
+	return 0;
+}
+
+#endif

+ 81 - 0
运算符重载2/5、关系运算符重载.cpp

@@ -0,0 +1,81 @@
+//4.5.5   关系运算符重载
+
+//作用 : 重载关系运算符,可以让两个自定义类型对象进行对比操作
+
+#if(0)
+
+#include <iostream>
+
+class Person
+{
+public:
+	
+	Person(std::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 false;
+		}
+		return true;
+	}
+
+	std::string m_Name;
+	int m_Age;
+
+
+};
+
+void test01()
+{
+	Person p1("Tom", 18);
+
+	Person p2("Tom", 18);
+
+	if (p1 == p2)
+	{
+		std::cout << "p1 和 p2是相等的" << std::endl;
+	}
+	else
+	{
+		std::cout << "p1 和 p2是不相等的" << std::endl;
+	}
+
+	if (p1 != p2)
+	{
+		std::cout << "p1 和 p2是不相等的" << std::endl;
+	}
+	else
+	{
+		std::cout << "p1 和 p2是相等的" << std::endl;
+	}
+}
+
+
+int main()
+{
+	
+	test01();
+
+	system("pause");
+
+	return 0;
+}
+
+#endif

+ 81 - 0
运算符重载2/6、函数调用运算符重载.cpp

@@ -0,0 +1,81 @@
+//4.5.6    函数调用运算符重载
+
+//⚪函数调用运算符 () 也可以重载
+
+//⚪由于重载后使用的方式非常像函数的调用,因此称为仿函数
+
+//⚪仿函数没有固定写法,非常灵活
+
+
+
+#include <iostream>
+
+//打印输出类
+
+class MYPrint
+{
+public:
+	//重载函数调用运算符
+	void operator()(std::string test)
+	{
+		std::cout << test << std::endl;
+	}
+
+
+};
+
+void MyPrint02(std::string test)
+{
+	std::cout << test << std::endl;
+
+}
+
+
+void test01()
+{
+	MYPrint myPrint;
+
+	myPrint("Hello World");          //由于使用起来非常类似于函数调用,因此成为仿函数。
+
+	MyPrint02("hello world");
+}
+
+//仿函数非常灵活,没有固定的写法
+
+//加法类
+
+class MyAdd
+{
+public:
+
+	int operator()(int num1,int num2)
+	{
+
+		return num1 + num2;
+	}
+};
+
+void test02()
+{
+	MyAdd myadd;
+	
+	int ret = myadd(100, 100);
+
+	std::cout << "ret = " << ret << std::endl;
+
+
+	//匿名函数对象
+	std::cout << MyAdd()(100, 100) << std::endl;
+
+}
+
+int main()
+{
+
+	//test01();
+	test02();
+
+	system("pause");
+
+	return 0;
+}