文章目录
- 运算符重载
- 1.+运算符重载
- 2.<<运算符重载
- 3.++运算符重载
- 4.=运算符重载
- 5.==和!=运算符重载
- 6.()运算符重载
运算符重载
1.+运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>using namespace std;class Person
{
public:/*+类内重载*/ //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;
};/*全局函数重载*/
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;
}void test1()
{Person p1;p1.m_A = 10;p1.m_B = 20;Person p2;p2.m_A = 100;p2.m_B = 200;cout << "p1.m_A = " << p1.m_A << endl;cout << "p1.m_B = " << p1.m_B << endl;cout << "p2.m_A = " << p2.m_A << endl;cout << "p2.m_B = " << p2.m_B << endl;Person p3 = p1 + p2;cout << "p3.m_A = " << p3.m_A << endl;cout << "p3.m_B = " << p3.m_B << endl;return;
}int main()
{test1();return 0;
}
运行结果:
2.<<运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>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:int m_A;int m_B;
};/*只能利用全局函数来重载<<运算符*/
/*利用成员函数重载 左移运算符 p.operator<<(cout) 简化 就是 p<<cout 不符合要求*/
ostream& operator<<(ostream &cout, Person &p)
{cout << "p.m_A = " << p.m_A << endl << "p.m_B = " << p.m_B;return cout;/*链式编程思想 返回一个ostream& (输出流)*/
}void test1()
{Person p(10, 20);cout << p << endl;/*<< 重载过后可以直接输出p的m_A和m_B的值*/return;
}int main()
{test1();return 0;
}
运行结果:
3.++运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>using namespace std;class MyType
{
public:MyType(){m_A = 0;}/*重载前置++运算符--返回引用是为了累加时,是对同一个变量操作*/MyType& operator++(){m_A++;return *this;}/*重载后置++运算符--返回引用是为了累加时,是对同一个变量操作*//*1.int 代表占位参数,可以用于区分前置递增和后置递增*//*2.返回值没有返回引用,是因为局部变量在函数结束时,就要释放掉*/MyType operator++(int){/*先记录值*/MyType temp = *this;/*完成递增*/m_A++;/*返回记录值*/return temp;}int m_A;
};ostream& operator<<(ostream& cout, MyType &m)
{cout << "m_A = " << m.m_A;return cout;
}void test1()
{MyType m;cout << ++m << endl;cout << ++m << endl;cout << m << endl;return;
}void test2()
{MyType m;m++;cout << m << endl;return;
}int main()
{//test1();test2();return 0;
}
运行结果:(test1函数运行)
运行结果:(test2函数运行)
4.=运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>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 != NULL){delete m_Age;m_Age = NULL;}/*深拷贝*/m_Age = new int(*p.m_Age);/*返回对象本身*/return *this;}int* m_Age;
};void test1()
{Person p1(10);Person p2(20);Person p3(30);p3 = p2 = p1;cout << *p1.m_Age << endl;cout << *p2.m_Age << endl;cout << *p3.m_Age << endl;
}int main()
{test1();return 0;
}
运行结果:
5.==和!=运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>using namespace std;class ID
{
public:bool operator==(ID &p){if ((this->name == p.name) && (this->age == p.age)){return true;}return false;}bool operator!=(ID& p){if ((this->name == p.name) && (this->age == p.age)){return false;}return true;}string name;int age;
};void test1()
{ID s1;s1.name = "玛丽";s1.age = 20;ID s2;s2.name = "玛丽";s2.age = 20;if (s1 == s2){cout << "s1 和 s2 是相等的!" << endl;}else{cout << "s1 和 s2 是不相等的!" << endl;}if (s1 != s2){cout << "s1 和 s2 是不相等的!" << endl;}else{cout << "s1 和 s2 是相等的!" << endl;}return;
}int main()
{test1();return 0;
}
运行结果:
6.()运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>using namespace std;class Print
{
public:void operator()(string str)/*重载()运算符--也叫仿函数*/{cout << str << endl;}
};class Add
{
public:int operator()(int num1, int num2)/*重载()运算符*/{return (num1 + num2);}
};void test1()
{Print p;p("hello world!");Add a;cout << a(50, 50) << endl;/*匿名函数对象*/cout << Add()(10, 20) << endl;
}int main()
{test1();return 0;
}
运行结果: