目录
运算符重载 是对已有的运算符 指定新功能,不能创建新运算符。
运算符重载关键字operator
语法:operator@ (@表示被重载的运算符)
思路:
1、弄懂 函数的参数中参数个数取决于运算符是一元还是二元。
2、弄懂运算符左边的运算对象 是类的对象 还是其他.
类的对象:全局函数实现(一元是一个参数,二元是两个参数)
成员函数实现(一元没有参数,二元是一个参数)--推荐
其他:只能是全局函数实现



- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- friend ostream& operator<<(ostream& out, Person ob);
- friend istream& operator>>(istream &in, Person &ob);
- friend Person operator+(Person ob1, Person ob2);
- private:
- int num;
- string name;
- float score;
- public:
- Person() {}
- Person(int num, string name, float score) :num(num), name(name), score(score) {}//初始化列表
- };
- ostream& operator<<(ostream& out, Person ob)
- {
- out << ob.num << " " << ob.name << " " << ob.score << endl;
- return out;
- }
- istream& operator>>(istream &in, Person &ob)
- {
- in >> ob.num >> ob.name >> ob.score;
- return in;
- }
- Person operator+(Person ob1, Person ob2)
- {
- Person tmp;
- tmp.num = ob1.num + ob2.num;
- tmp.name = ob1.name + ob2.name;
- tmp.score = ob1.score + ob2.score;
- return tmp;
- }
- int main(int argc, char* argv[])
- {
- Person lucy(100, "lucy", 85.5f);
- Person bob(101, "bob", 95.5f);
- Person tom(102, "tom", 75.5f);
- cout << lucy + bob + tom << endl;
- return 0;
- }
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- friend ostream& operator<<(ostream& out, Person ob);
- friend istream& operator>>(istream &in, Person &ob);
- private:
- int num;
- string name;
- float score;
- public:
- Person() {}
- Person(int num, string name, float score) :num(num), name(name), score(score) {}//初始化列表
- //成员函数重载+
- Person operator+(Person ob)
- {
- Person tmp;
- tmp.num = this-> num + ob.num;
- tmp.name = this-> name + ob.name;
- tmp.score = this-> score + ob.score;
- return tmp;
- }
- };
- ostream& operator<<(ostream& out, Person ob)
- {
- out << ob.num << " " << ob.name << " " << ob.score << endl;
- return out;
- }
- istream& operator>>(istream &in, Person &ob)
- {
- in >> ob.num >> ob.name >> ob.score;
- return in;
- }
- int main(int argc, char* argv[])
- {
- Person lucy(100, "lucy", 85.5f);
- Person bob(101, "bob", 95.5f);
- Person tom(102, "tom", 75.5f);
- cout << lucy + bob + tom << endl;
- return 0;
- }
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- friend ostream& operator<<(ostream& out, Person ob);
- friend istream& operator>>(istream &in, Person &ob);
- private:
- int num;
- string name;
- float score;
- public:
- Person() {}
- Person(int num, string name, float score) :num(num), name(name), score(score) {}//初始化列表
- //成员函数重载==
- bool operator==(Person& ob)
- {
- if (num == ob.num && name == ob.name && score == ob.score)
- return true;
- return false;
- }
- };
- ostream& operator<<(ostream& out, Person ob)
- {
- out << ob.num << " " << ob.name << " " << ob.score << endl;
- return out;
- }
- istream& operator>>(istream &in, Person &ob)
- {
- in >> ob.num >> ob.name >> ob.score;
- return in;
- }
- int main(int argc, char* argv[])
- {
- Person lucy(100, "lucy", 85.5f);
- Person bob(101, "bob", 95.5f);
- if (lucy == bob)
- {
- cout << "相等" << endl;
- }
- else
- {
- cout << "不相等" << endl;
- }
- return 0;
- }
++a(前置++),它就调用operator++(a)
a++(后置++),它就会去调用operator++(a,int)//int为占位参数。
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- friend ostream& operator<<(ostream& out, Person ob);
- friend istream& operator>>(istream &in, Person &ob);
- private:
- int num;
- string name;
- float score;
- public:
- Person() {}
- Person(int num, string name, float score) :num(num), name(name), score(score) {}//初始化列表
- //重载后置++ 普通全局函数为operator++(a,int)
- //成员函数
- Person operator++(int)
- {
- //先保存 旧的值
- Person old = *this;//*this == lucy
- //lucy++自增
- this-> num = this-> num + 1;
- this-> name = this-> name + this-> name;//(自定义操作)
- this-> score = this-> score + 1;
- return old;//返回旧值
- }
- };
- ostream& operator<<(ostream& out, Person ob)
- {
- out << ob.num << " " << ob.name << " " << ob.score << endl;
- return out;
- }
- istream& operator>>(istream &in, Person &ob)
- {
- in >> ob.num >> ob.name >> ob.score;
- return in;
- }
- int main(int argc, char* argv[])
- {
- Person lucy(100, "lucy", 85.5f);
- Person bob;
- //先使用 后++
- bob = lucy++;
- cout << bob << endl;
- cout << lucy << endl;
- return 0;
- }
- #include<iostream>
- #include<string>
- using namespace std;
- class Person
- {
- friend ostream& operator<<(ostream& out, Person ob);
- friend istream& operator>>(istream &in, Person &ob);
- private:
- int num;
- string name;
- float score;
- public:
- Person() {}
- Person(int num, string name, float score) :num(num), name(name), score(score) {}//初始化列表
- //重载后置++ 普通全局函数为operator++(a,int)
- //成员函数
- Person operator++(int)
- {
- //先保存 旧的值
- Person old = *this;//*this == lucy
- //lucy++自增
- this-> num = this-> num + 1;
- this-> name = this-> name + this-> name;//(自定义操作)
- this-> score = this-> score + 1;
- return old;//返回旧值
- }
- //重载前置++ 普通全局函数为operator++(a)
- //成员函数
- Person operator++(int)
- {
- //先++
- this-> num = this-> num + 1;
- this-> name = this-> name + this-> name;//(自定义操作)
- this-> score = this-> score + 1;
- //后使用
- return *this;
- }
- };
- ostream& operator<<(ostream& out, Person ob)
- {
- out << ob.num << " " << ob.name << " " << ob.score << endl;
- return out;
- }
- istream& operator>>(istream &in, Person &ob)
- {
- in >> ob.num >> ob.name >> ob.score;
- return in;
- }
- int main(int argc, char* argv[])
- {
- Person lucy(100, "lucy", 85.5f);
- Person bob;
- ///先++ 后使用
- bob = ++lucy;
- cout << bob << endl;
- cout << lucy << endl;
- return 0;
- }
重载()运算符 一般用于 为算法 提供策略。

string类完成重载输入输出、重载中括号运算符、重载+运算符、重载=赋值运算符(深拷贝)、重载>运算符等运算
mystring.h
- #ifndef MYSTRING_H
- #define MYSTRING_H
- #include <iostream>
- using namespace std;
- class MyString
- {
- friend ostream& operator<<(ostream &out, MyString ob);
- friend istream& operator>>(istream &in, MyString &ob);
- private:
- char *str;
- int size;
- public:
- MyString();
- MyString(char *str);
- MyString(const MyString &ob);//构造拷贝
- ~MyString();
- int getSize() const;
- //成员函数重载[]
- char& operator[](int pos);
- MyString operator+(MyString ob);
- MyString operator+(char *str);
-
- MyString& operator=(MyString ob);
- MyString& operator=(char *str);
-
- bool operator>(MyString ob);
- bool operator>(char *str);
-
- // bool operator<(MyString ob);
- // bool operator<(char *str);
- // bool operator==(MyString ob);
- // bool operator==(char *str);
- // bool operator!=(MyString ob);
- // bool operator!=(char *str);
- };
-
- #endif // MYSTRING_H
mystring.cpp
- #include "mystring.h"
- #include <string.h>
-
- int MyString::getSize() const
- {
- return size;
- }
-
- char& MyString::operator[](int pos)
- {
- if(pos<0 || pos>=size)
- {
- cout<<"元素位置不合法"<<endl;
- exit(‐1);
- }
- return str[pos];
- }
-
- MyString MyString::operator+(MyString ob)
- {
- MyString tmp;
- tmp.size = size+ob.size;
- tmp.str = new char[tmp.size+1];
- memset(tmp.str, 0, tmp.size+1);
-
- strcpy(tmp.str, str);
- strcat(tmp.str, ob.str);
-
- return tmp;
- }
-
- MyString MyString::operator+(char *str)
- {
- MyString tmp;
- tmp.size = size+strlen(str);
- tmp.str = new char[tmp.size+1];
- memset(tmp.str, 0, tmp.size+1);
-
- strcpy(tmp.str, this‐>str);
- strcat(tmp.str, str);
-
- return tmp;
- }
-
- MyString &MyString::operator=(MyString ob)
- {
- //str2 = str1;
- if(this‐>str != NULL)
- {
- delete [] this‐>str;
- this‐>str = NULL;
- }
- this‐>size = ob.size;
- this‐>str = new char[this‐>size+1];
- memset(this‐>str, 0, this‐>size+1);
- strcpy(this‐>str, ob.str);
-
- return *this;
- }
-
- MyString &MyString::operator=(char *str)
- {
- //str2 = str1;
- if(this‐>str != NULL)
- {
- delete [] this‐>str;
- this‐>str = NULL;
- }
-
- this‐>size = strlen(str);
- this‐>str = new char[this‐>size+1];
- memset(this‐>str, 0, this‐>size+1);
- strcpy(this‐>str, str);
-
- return *this;
- }
-
- bool MyString::operator>(MyString ob)
- {
- if(str==NULL || ob.str == NULL)
- {
- exit(‐1);
- }
- if(strcmp(this‐>str, ob.str) > 0)
- {
- return true;
- }
- return false;
- }
-
- bool MyString::operator>(char *str)
- {
- if(this‐>str==NULL || str == NULL)
- {
- exit(‐1);
- }
- if(strcmp(this‐>str, str) > 0)
- {
- return true;
- }
- return false;
- }
-
- MyString::MyString()
- {
- str=NULL;
- size=0;
- }
-
- myString::MyString(char *str)
- {
- size = strlen(str);
- this‐>str = new char[size+1];
- memset(this‐>str, 0, size+1);
-
- strcpy(this‐>str, str);
- }
-
- MyString::MyString(const MyString &ob)
- {
- size = ob.size;
- str = new char[size+1];
- memset(str, 0, size+1);
- strcpy(str, ob.str);
- }
-
- myString::~MyString()
- {
- if(str != NULL)
- {
- delete [] str;
- str=NULL;
- }
- }
-
- //全局函数实现 <<重载
- ostream& operator<<(ostream &out, MyString ob)
- {
- out<<ob.str;
- return out;
- }
- //全局函数实现 >>重载
- istream& operator>>(istream &in, MyString &ob)
- {
- char buf[1024]="";
- cin>>buf;
- if(ob.str != NULL)//ob已经有字符串
- {
- delete [] ob.str;
- ob.str = NULL;
- }
-
- ob.size = strlen(buf);
- ob.str = new char[ob.size+1];
- memset(ob.str, 0,ob.size+1);
- strcpy(ob.str, buf);
-
- return in;
- }
智能指针:通过重载* 和->运算符。解决 堆区空间的对象 释放问题
- class Data
- {
- public:
- Data()
- {
- cout<<"无参构造"<<endl;
- }
- ~Data()
- {
- cout<<"析构函数"<<endl;
- }
- void func()
- {
- cout<<"func函数"<<endl;
- }
- };
- class SmartPointer//智能指针类
- {
- private:
- Data *p;
- public:
- SmartPointer(){}
- SmartPointer(Data *p)
- {
- this‐>p = p;
- }
- ~SmartPointer()
- {
- delete p;
- }
- //重载*和->
- Data& operator*()
- {
- return *p;
- }
- Data* operator‐>()
- {
- return p;
- }
- };
-
- void test02()
- {
- SmartPointer ob(new Data);
- (*ob).func();//ob.operator *().func();
-
- ob‐>func();//ob.operator ‐>()‐>func();
- }