- 什么是运算符重载
- 赋予运算符具有操作自定义类型数据功能
- 运算符重载的实质是什么
- 运算符重载的实质本身就是函数调用
- 运算符重载函数的写法
函数返回值 函数名(函数参数)
函数返回值:运算符运算完成后的值决定的 Complex
函数名 :operator加上重载运算符组成函数名 operator+
参数 :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
函数体 :学运算符具体想要的操作
- 运算符重载函数的写法
- 运算符重载的实质本身就是函数调用
- C++运算符重载
- 前言
- 一、友元函数重载运算符
- 二、类成员函数重载运算符
- 三、特殊运算符重载
- 1.流运算符重载
- 2.++ --运算符重载
- 3.文本重载
- 4.其他运算符
- 四、对象隐式转换
- 五、重载综合案例
提示:以下是本篇文章正文内容,下面案例可供参考
一、友元函数重载运算符#includeusing namespace std; class Complex { public: Complex(int a=0, int b=0) :a(a), b(b) { } void print() { cout << a << endl; cout << b << endl; } friend Complex operator+ (Complex one, Complex two); protected: int a; int b; }; //友元重载:参数个数就是操作数据 Complex operator+ (Complex one, Complex two) { return Complex(one.a + two.a, one.b + two.b); } int main() { Complex one(1, 1); Complex two(2, 0); Complex three; three = one + two; //Complex 重载函数的隐式调用 three.print(); //显式调用 Complex result; result = operator+(one, two); while (1); return 0; }
运算结果:
#includeusing namespace std; class Complex { public: Complex(int a=0, int b=0) :a(a), b(b){} void print() { cout << a << endl; cout << b << endl; } friend Complex operator+ (Complex one, Complex two); //类成员函数重载,参数个数等于操作减一 bool operator> (Complex object) { if (this->a > object.a) { return true; } else if (this->a == object.a&&this->b > object.b) { return true; } else return false; } protected: int a; int b; }; //友元重载:参数个数就是操作数据 Complex operator+ (Complex one, Complex two) { return Complex(one.a + two.a, one.b + two.b); } int main() { Complex one(1, 1); Complex two(2, 0); Complex three; three = one + two; //Complex 重载函数的隐式调用 three.print(); //显式调用 Complex result; result = operator+(one, two); if (one > two) //one>two是bool { cout << "one比较大" << endl; } if (one.operator> (two)) { cout << "one比较大" << endl; } while (1); return 0; }
运算结果:
- cin类型:istream类的对象
- cout类型:ostream类的对象
- 流运算符>> <<
- 必须采用友元函数的形式重载
#include#include using namespace std; class STU { public: STU(string name="", int age=18) :name(name), age(age){} friend istream& operator>>(istream& in, STU& lisi); friend ostream& operator<<(ostream& out, STU& lisi); protected: string name; int age; }; //其他运算符 //=()-》[]只能采用类的成员函数形式重载 //流重载采用友元方式 //. .* ?: :: 不能被重载 istream& operator>>(istream& in, STU& lisi) { in >> lisi.name >> lisi.age; return in; } ostream& operator<<(ostream& out, STU& lisi) { out << lisi.name << "t"< > str; cout << str; STU lisi; cin >> lisi; //void operator>>(istream& in,STU& lisi) cout << lisi; //void operator<<(ostream& out,STU& lisi) cin >> str >> lisi; cout << str << endl << lisi; while (1); return 0; }
运算结果:
- 解决问题:前置和后置的问题:
- 增加无用参数 int去表示当前运算符重载是后置操作
- =()-》[]只能采用类的成员函数形式重载
- 流重载采用友元方式
- . .* ?: :: 不能被重载
#include#include using namespace std; class STU { public: STU(string name, int age) :name(name), age(age){} friend ostream& operator<<(ostream& out, STU& object) { out << object.name << "t" < 运行结果:
四、对象隐式转换
#includeusing namespace std; class STU { public: //类的对象的隐式转换 operator operator int() { return age; } STU(string name, int age):name(name), age(age){} protected: string name; int age; }; int main() { STU lisi("李四", 18); int lisiage = lisi; cout << lisiage << endl; while (1); return 0; } 运行结果:
五、重载综合案例
在这里插入图片描述](https://img-blog.csdnimg.cn/4fc44c73b0ef4c9dafef44a71f970ff3.png)#include#include #include #include using namespace std; class Int { public: Int(int num) :num(num) {} int& data() { return num; } string tostr() { return to_string(num); } //算术运算符重载 Int operator+(const Int& value) { return Int(this->num + value.num); } //友元重载 :操作数-1 等于重载函数的参数个数 friend Int operator-(const Int& one, const Int& two) { //operator-(one,two); return Int(one.num - two.num); } Int operator+=(const int& value) { return Int(this->num + value); } Int operator+=(const Int& value) { return Int(this->num + value.num); } Int operator++(int) { return Int(this->num++); } Int operator++() { return Int(++this->num); } Int operator&(const Int& value) { return Int(this->num & value.num); } bool operator!() { return !this->num; } Int operator-() { return Int(-this->num); } friend ostream& operator<<(ostream& out, const Int& object) { out << object.num << endl; return out; } friend istream& operator>>(istream& in, Int& object) { in >> object.num; return in; } int* operator&() { return &this->num; } bool operator>(const Int& object) { return this->num > object.num; } protected: int num; }; void print(const int& num) { cout << num << endl; } //重载[] class myvector { public: myvector(int size) { base = new int[size] {0}; } int& operator[](int index) { return base[index]; } protected: int *base; }; //重载()运算符 class Function { typedef void(*PF)(); public: Function(PF pf) :pf(pf) {} void operator()() { pf(); } protected: PF pf; }; //-> struct MM { string name; int age; MM(string name, int age) :name(name), age(age) {} }; class Auto_ptr { public: Auto_ptr(int* ptr) :ptr(ptr) {} Auto_ptr(MM* ptr) :ptrMM(ptr) {} int& operator*() { return *ptr; } MM* operator->() { return ptrMM; } ~Auto_ptr() { if (ptrMM) { delete ptr; ptr = nullptr; } if (ptrMM) { delete ptrMM; ptrMM = nullptr; } } protected: int* ptr; MM* ptrMM; }; void testAuto_ptr() { //int* pInt = new int(18); //Auto_ptr ptr(pInt); //上面两行等效下面一行 Auto_ptr ptr(new int(18)); cout << *ptr << endl; Auto_ptr ptrMM(new MM("mm", 19)); cout << ptrMM->name<< endl; cout << ptrMM->age << endl; auto_ptr autoPtr(new int(18)); cout << *autoPtr << endl; auto_ptr autoPtrMM(new MM("mm", 19)); cout << autoPtrMM->name << endl; cout << autoPtrMM->age << endl; } void print() { cout << "测试函数" << endl; } int max(int a, int b) { return a + b; } int main() { print(1); int a = 1; print(a); Int num(10); cout << num; cout << -num; myvector vec(5); #if 0 for (int i = 0; i < 5; i++) { cin >> vec[i]; } for (int i = 0; i < 5; i++) { cout << vec[i]; } #endif Function p(print); p(); //function pf(max); //cout << pf(1, 2) << endl; testAuto_ptr(); while (1); return 0; } 运行结果:



