#include <bits/stdc++.h> 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 &p,int num) { Person temp; temp.m_A = p.m_A + num; temp.m_B = p.m_B + num; return temp; } void test() { Person p1; p1.m_A = 10; p1.m_B = 10; Person p2; p2.m_A = 10; p2.m_B = 10; //成员函数重载+本质 Person p3 = p1.operator+(p2); //2.全局函数重载+本质 Person 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() { test(); return 0; } #include <bits/stdc++.h> 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: //利用成员函数重载 左移运算符 //不会用成员函数重载<<运算符,因为无法实现cout 在左侧 /*void operator<<(Person &p) { }*/ int m_A; int m_B; }; //只能利用全局函数实现左移运算符 ostream &operator<<(ostream &cout,Person &p) //本质 operator<<(cout,p) 简化cout<<p { cout << "m_A = " << " " << p.m_A << "m_B = " << p.m_B << endl; return cout; } void test() { Person p(10,10); //p.m_A = 10; //p.m_B = 10; //cout << p.m_A << endl; cout << p << endl; } int main() { test(); return 0; } #include <bits/stdc++.h> 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 test1() { MyInteger myint; cout << ++myint << endl; } void test2() { MyInteger myint; cout << myint++ << endl; cout << myint << endl; } int main() { //test1(); test2(); //int a = 0; //cout << ++(++a) << endl; //cout << a << endl; return 0; } #include <bits/stdc++.h> 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) { //编译器提供浅拷贝 //m_Age = p.m_Age; //应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝 if(m_Age != NULL) { delete m_Age; m_Age = NULL; } //深拷贝 new int(*p.m_Age); //返回对象本身 return *this; } int *m_Age; }; void test() { Person p1(18); Person p2(20); Person p3(30); p2 = p1; //赋值操作 cout << "p1的年龄为: " << *p1.m_Age << endl; cout << "p2的年龄为: " << *p2.m_Age << endl; cout << "p3的年龄为: " << *p3.m_Age << endl; } int main() { test(); return 0; } #include <bits/stdc++.h> 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; } else { return false; } } //重载!=号 bool operator!=(Person &p) { if(this->m_Name == p.m_Name && this->m_Age == p.m_Age) { return false; } else { return true; } } string m_Name; int m_Age; }; void test() { Person p1("TOM",18); Person p2("Tom",18); if(p1 == p2) { cout << "p1和p2相等" << endl; } else { cout << "p1和p2不相等" << endl; } if(p1 != p2) { cout << "p1和p2不相等" << endl; } else { cout << "p1和p2相等" << endl; } } int main() { test(); return 0; } #include <bits/stdc++.h> using namespace std; //函数调用运算符的重载 class MyPrint { public: //重载函数调用运算符 void operator()(string test) { cout << test << endl; } }; void test() { MyPrint myPrint; myPrint("Hello World!"); //使用类似于函数调用 } //加法类 class MyAdd { public: int operator()(int num1,int num2) { return num1+num2; } }; void test2() { MyAdd myadd; int ret = myadd(100,200); cout << "ret = " << ret << endl; //匿名函数对象 cout << MyAdd()(100,200) << endl; } int main() { test(); test2(); return 0; }