运算符重载(Operator Overloading):同一个运算符可以有不同的功能。
通过运算符重载,扩大了C++已有运算符的功能,使之能用于对象。
加号运算符重载
把两个对象的成员分别相加,返回最终的对象
#include<iostream>
using namespace std;
//加号运算符重载
//1.成员函数做加号运算符重载
class P {
public:
int m_A;
int m_B;
P() {};
P(int a, int b);
//重载运算符有一个返回类型和一个参数列表
//P operator+(P& p) 对象作为参数进行传递
//{
// P temp;
// temp.m_A = this->m_A + p.m_A; 对象的属性使用 this 运算符进行访问
// temp.m_B = this->m_B + p.m_B;
// return temp;
//}
};
P::P(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//2.全局函数做加号运算符重载
P operator+(P& p1, P& p2)
{
P temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
void test01()
{
P p1(4, 8);
P p2(6, 2);
P p3 = p1 + p2; //可以直接用operator+,也可以用+号
cout << p3.m_A << endl;
cout << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
左移运算符重载
cout 是 ostream 类的对象,cin 是 istream 类的对象
#include<iostream>
using namespace std;
//左移运算符重载
class P {
//了能够直接访问private 成员变量,同样需要将该函数声明为友元函数:
friend ostream& operator<< (ostream& cout, P& p);
private:
int m_A;
int m_B;
public:
P(int a, int b) {
m_A = a;
m_B = b;
}
};
//只能用全局函数来重载左移运算符
//由于cout全局只能有一个,所以要采用引用的方式去拿到
ostream& operator<< (ostream& cout, P& p) {
cout << "p.m_A的值为:" << p.m_A << " p.m_B的值为:" << p.m_B;
return cout;//返回了对象的引用,所以重载后的运算符可以实现连续输出
}
int main() {
P p(10, 20);
cout << p << " 你好呀" << endl;
system("pause");
return 0;
}
重载++和--运算符
自增++
和自减--
都是一元运算符,它的前置形式和后置形式都可以被重载。
#include<iostream>
using namespace std;
class MyInterger {
friend ostream& operator<<(ostream& cout, MyInterger myInt);
public:
MyInterger() {
m_Int = 0;
}
//重载前置++运算符
MyInterger& operator++() {
m_Int++;
return *this;
}
//重载后置++运算符
//int表示占位参数,可以用来区分前置和后置运算符
MyInterger operator++(int) {
MyInterger temp = *this;//先记录当前结果,再进行++操作
m_Int++;
return temp;
}
//重载前置--运算符
MyInterger& operator--() {
m_Int--;
return *this;
}
//重载后置--运算符
//int表示占位参数,可以用来区分前置和后置运算符
MyInterger operator--(int) {
MyInterger temp = *this;//先记录当前结果,再进行--操作
m_Int--;
return temp;
}
private:
int m_Int;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInterger myInt) {
cout << myInt.m_Int;
return cout;
}
void test01() {
MyInterger myInt;
cout << ++myInt << endl;
cout << ++myInt << endl;
cout << myInt << endl;
}
void test02() {
MyInterger myInt;
cout << myInt++ << endl;
cout << myInt++ << endl;
cout << myInt << endl;
}
void test03() {
MyInterger myInt;
cout << --myInt << endl;
cout << --(--myInt) << endl;
cout << myInt << endl;
}
void test04() {
MyInterger myInt;
cout << myInt-- << endl;
cout << (myInt--)-- << endl;
cout << myInt << endl;
}
int main() {
cout << "前置++" << endl;
test01();
cout << "后置++" << endl;
test02();
cout << "前置--" << endl;
test03();
cout << "后置--" << endl;
test04();
system("pause");
return 0;
}
赋值运算符重载
#include<iostream>
using namespace std;
//赋值运算符重载
class Person {
public:
int* m_Age;
Person(int age) {
m_Age = new int(age);
}
~Person() {
if (m_Age == NULL) {
return;
}
else {
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;
}
};
void test01() {
Person p(18);
Person p3(20);
Person p2(30);
p3 = p = p2;
cout << "p的年龄为:" << *p.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
关系运算符重载
#include <iostream>
using namespace std;
class Person {
public:
Person(string name, int age) {
m_Name = name;
m_Age = age;
}
string m_Name;
int m_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;
}
}
};
void test01() {
Person p1("张三", 18);
Person p2("张三", 34);
if (p1 != p2) {
cout << "p1和p2是不相等的" << endl;
}
else {
cout << "p1和p2是相等的" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
函数调用运算符重载
- 函数调用运算符()也可以进行重载
- 由于重载后的使用方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活