1.介绍
运算符重载是对已有的运算符重新定义,赋予不同的功能,以适应不同的数据类型。
2.归类
加号运算符重载代码实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Person {
public:
Person() {
cout << "构造函数";
};
Person(int age, int weight) {
this->age = age;
this->weight = weight;
}
Person operator+(const Person& p) {//成员函数实现+号运算符重载
Person temp;
temp.age = this->age + p.age;
temp.weight = this->weight + p.weight;
return temp;
}
int age, weight;
};
//全局函数实现+号运算符重载
Person operator+(const Person& p1, const Person& p2) {
Person temp;
temp.age = p1.age + p2.age;
temp.weight = p1.weight + p2.weight;
return temp;
}
int main() {
Person p1(10, 10);
Person p2(100, 100);
Person p3;
p3 = p1 + p2;
//p3 = p1.operator+(p2);//与上同义
cout << p3.age << " " << p3.weight;
}
左移运算符重载代码实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Person {
public:
Person(int age, int weight) {
this->age = age;
this->weight = weight;
}
int age, weight;
};
ostream& operator<<(ostream& cout, Person& p) {//<<运算符的重载只能在全局函数中实现
//由于每次只能cout一个对象,利用链式编程思想即可实现,cout的类型是ostream,查看底层就能知道
cout << p.age << " " << p.weight << endl;
return cout;
}
int main() {
Person p1(10, 20);
Person p2(30, 40);
cout << p1 << p2 << endl;
}
递增运算符代码实现:(此处将左移运算符重载与递增运算符重载共同使用)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Person {
public:
Person() {};
Person(int age,int weight) {
this->age = age;
this->weight = weight;
}
Person& operator++() {//前置递增
this->age++;
this->weight++;
return *this;
}
Person operator++(int) {//后置递增,int作为占位符用来区分两个函数
Person temp=*this;//先将temp赋值为原来的
this->age++;
this->weight++;
return temp;//返回的是一个临时对象,返回之后会被释放,所以不能加上引用。
}
int age, weight;
};
ostream& operator<<(ostream& cout, Person p) {
cout << p.age << " " << p.weight << endl;
return cout;
}
int main() {
Person p1(5,5);
++p1;
cout << p1 << endl;
p1++;
cout << p1 << endl;
}
附带递减运算符重载:
class Person {
public:
Person() {};
Person(int age, int weight) {
this->age = age;
this->weight = weight;
}
Person& operator--() {
this->age--;
this->weight--;
return *this;
}
Person operator--(int) {
Person temp = *this;
this->age--;
this->weight--;
return temp;
}
int age, weight;
};
关系运算符重载代码实现:(包括==和!=)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Person {
public:
Person(int age, int weight) {
this->age = age;
this->weight = weight;
}
bool operator==(Person& p) {
if (this->age == p.age && this->weight == p.weight)
return 1;
else return 0;
}
bool operator!=(Person& p) {
if (this->age == p.age && this->weight == p.weight)
return 0;
else return 1;
}
int age, weight;
};
int main() {
Person p1(10, 20);
Person p2(10, 20);
if (p1 == p2)cout << "yes" << endl;
else cout << "no" << endl;
if (p1 != p2)cout << "yes" << endl;
else cout << "no" << endl;
}
函数调用运算符重载:
(主要用于仿函数,下面利用仿函数对set容器进行自定义类型的自定义规则·排序)
#include<iostream>
#include<set>
#include<ctime>
#include<cstdio>
#include <stdlib.h>
using namespace std;
class Person {
public:
Person(int age, int weight) {
this->age = age;
this->weight = weight;
}
int age, weight;
};
class mycompare {
public:
bool operator()(Person a, Person b)const {//函数调用运算符重载
if (a.age != b.age)//按照age从大到小排序,若相等,按照weight从大到小
return a.weight > b.weight;
else return a.age > b.age;
}
};
int main() {
srand((unsigned int)time(NULL));
set<Person, mycompare>cont;
for (int i = 1; i <= 10; i++) {
int weight = rand() % 41 + 60;
Person temp(i, weight);
cont.insert(temp);
}
for (set<Person, mycompare>::iterator it = cont.begin(); it != cont.end(); it++)
cout << it->age << " " << it->weight << endl;//最终结果输出
}