-
什么是运算符重载
-
赋予运算符具有操作自定义类型数据功能
- 运算符重载的实质本身就是函数调用
- 运算符重载函数的写法
函数返回值 函数名(函数参数) 函数返回值 : 运算完成后的值决定的 函数名 : operator 加上重载运算符组成函数名 operator+ 参数 : 看运算符的操作数,具体参数个数是要看你重载函数形式是什么 函数体 : 写运算符具体想要的操作
-
-
友元函数重载运算符
-
#include<iostream> #include <string> using namespace std; class point { public: point(int a=0,int b=0):a(a),b(b){} void print() { cout <<"(" << a <<"," << b << ")" << endl; } friend point operator+(const point& one, const point& two); protected: int a; int b; private: }; point operator+(const point& one, const point& two) { return point(one.a + two.a, one.b + two.b); } int main() { point p1(1, 1); point p2(2, 3); point p3; p3 = p1 + p2;//重载函数的隐式调用 p3.print(); p3 = operator+(p1, p2);//重载函数的显式调用 p3.print(); return 0; }
-
-
类成员函数重载运算符
-
#include<iostream> #include <string> using namespace std; class point { public: point(int a = 0, int b = 0) :a(a), b(b) {} void print() { cout << "(" << a << "," << b << ")" << endl; } bool operator<(const point& object); protected: int a; int b; private: }; bool point::operator<(const point& object) { if (this->a<object.a) { return true; } else if (this->a==object.a&&this->b<object.b) { return true; } else { return false; } } int main() { point p1(2, 4); point p2(2, 3); if (p1 < p2) { cout << "p1比p2小" << endl; } else { cout << "p1不比p2小" << endl; } return 0; }
-
-
特殊运算符重载
-
流运算符重载
-
cin类型 : istream类的对象
-
cout类型:ostream类的对象
-
流运算符 >> << 必须采用友元函数形式重载
-
#include<iostream> #include <string> using namespace std; class Heroes { public: Heroes(string name="", int level = 0) :name(name), level(level) {} friend ostream& operator<<(ostream& out, Heroes& hero); friend istream& operator>>(istream& in, Heroes& hero); protected: string name; int level; private: }; ostream& operator<<(ostream& out, Heroes& hero) { out << hero.name << " " << hero.level << endl; return out; } istream& operator>>(istream& in, Heroes& hero) { in >> hero.name >> hero.level; return in; } int main() { Heroes hero1("赵信", 18); cout << hero1 << endl; Heroes hero2; cin >> hero2; cout << hero2 << endl; return 0; }
-
-
++ --运算符重载
-
解决问题:前置和后置的问题:增加无用参数 int去表示当前运算符重载是后置操作
-
#include<iostream> #include <string> using namespace std; class Heroes { public: Heroes(string name = "", int level = 0) :name(name), level(level) {} void print(); Heroes operator++(int) { return Heroes(name, level++); } Heroes operator++() { return Heroes(name, ++level); } protected: string name; int level; private: }; void Heroes::print() { cout << name << " " << level << endl; } int main() { Heroes hero1("赵信",12); hero1++;//hero1.level=13 hero1.print(); Heroes hero2 = hero1++;//hero2.level=13 hero1.level=14 hero2.print(); Heroes hero3 = ++hero1;//hero1.level=15 hero3.level=15 hero3.print(); return 0; }
-
- 文本重载 (新标准中的,稍微落后一点开发工具不适用)
-
#include<iostream> #include <string> using namespace std; unsigned long long operator"" _h(unsigned long long num) { return 60 * 60 * num; } unsigned long long operator"" _min(unsigned long long num) { return 60 * num; } int main() { int second = 1_h; cout << second << "s" << endl; int sum = 1_h + 18_min + 30; cout << sum << "s" << endl; return 0; }
-
- 其他运算符
-
. .* ?: :: 不能重载
-
流重载采用友元方式
-
= () -> [] 只能采用类的成员函数形式重载
-
重载[ ]
#include<iostream> #include <string> using namespace std; class myVector { public: myVector(int num = 0) { arr = new int[num]; nSize = num; } int& size() { return nSize; } int& operator[](int index) { return arr[index]; } protected: int* arr; int nSize; private: }; int main() { myVector array(3); for (int i=0;i<array.size();i++) { array[i] = i; } for (int i = 0; i < array.size(); i++) { cout<<array[i]<<" "; } return 0; }
-
重载()
#include<iostream> #include <string> #include <functional> #include <memory> using namespace std; class Function { //函数指针的定义:函数返回值类型 (* 指针变量名) (函数参数列表); typedef void(*PF)();//这里相当于把void(*)()起别名为PF public: Function(PF pf) :pf(pf) {} void operator()() { pf(); } protected: PF pf;//函数指针 类型为void(*)() }; class nFunction { typedef int(*nPF)(int,int); public: nFunction(nPF pf):pf(pf) { } int operator()(int a,int b) { return pf(a,b); } protected: nPF pf;//函数指针 类型为void(*)() }; void print() { cout << "重载括号" << endl; } int Sum(int a, int b) { return a + b; } int main() { Function p(print); //让对象模仿函数的行为-->仿函数 p();//括号运算符的隐式调用 p.operator()();//括号运算符的显式调用 //cout << Sum(1, 2) << endl;; nFunction pf(Sum); //pf(1, 2) cout << pf(1, 2) << endl; //pf.operator()(1, 2); cout << pf.operator()(1, 2) << endl; return 0; }
-
重载->
#include<iostream> #include <string> using namespace std; //-> struct Heroes { string name; int level; Heroes(string name, int level) :name(name), level(level) {} }; //创建一个自动释放的指针类 class Auto_ptr { public: Auto_ptr(int* ptr) :ptr(ptr) {} Auto_ptr(Heroes* ptr) :ptrHero(ptr) {} int& operator*() { return *ptr; } Heroes* operator->() { return ptrHero; } ~Auto_ptr() { if (ptr) { delete ptr; ptr = nullptr; } if (ptrHero) { delete ptrHero; ptrHero = nullptr; } } protected: int* ptr; Heroes* ptrHero; }; int main() { //int* pInt = new int(18); //Auto_ptr ptr(pInt); //上面两行等效下面一行 Auto_ptr ptr(new int(18)); cout << *ptr << endl; Auto_ptr ptrHero(new Heroes("盖伦", 16)); cout << ptrHero->name << endl; cout << ptrHero->level << endl; //自动指针 auto_ptr<指针类型> 指针变量 auto_ptr<int> autoPtr(new int(18)); cout << *autoPtr << endl; auto_ptr<Heroes> autoPtrHero(new Heroes("盖伦", 16)); cout << autoPtrHero->name << endl; cout << autoPtrHero->level << endl; return 0; }
-
-
-
对象隐式转换
-
#include<iostream> #include <string> using namespace std; class Heroes { public: Heroes(string name = "", int level = 0) :name(name), level(level) {} void print(); //类的对象的隐式转换 operator operator int() { return level; } protected: string name; int level; private: }; void Heroes::print() { cout << name << " " << level << endl; } int main() { Heroes hero1("赵信",12); int levels = hero1; cout << levels << endl; return 0; }
-
-
-
封装一个Array类,实现定长数组的操作
-
#include<iostream> #include <string> using namespace std; class Array { public: Array(int num = 0) { arr = new int[num]; nSize = num; } Array(Array& object) { this->nSize = object.nSize; this->arr = new int[this->nSize]; for (int i = 0; i < object.size(); i++) { this->arr[i] = object.arr[i]; } } ~Array() { delete[]arr; arr = nullptr; } int& size() { return nSize; } int& operator[](int index) { return arr[index]; } friend istream& operator>>(istream& in, Array& object) { for (int i = 0; i < object.size(); i++) { in >> object.arr[i]; } return in; } friend ostream& operator<<(ostream& out, Array& object) { for (int i = 0; i < object.size(); i++) { out << object.arr[i]; } return out; } friend Array& operator+(const Array& one, const Array& two) { Array* temp = new Array(one.nSize+ two.nSize); int nCount = 0; for (int i = 0; i < one.nSize; i++) { temp->arr[nCount++] = one.arr[i]; } for (int i = 0; i < two.nSize; i++) { temp->arr[nCount++] = two.arr[i]; } return *temp; } void operator=(Array& object) { this->nSize = object.nSize; this->arr = new int[this->nSize]; for (int i = 0; i < object.size(); i++) { this->arr[i] = object.arr[i]; } } protected: int *arr; int nSize; private: }; int main() { //以下测试代码要能够成功运行 Array array(5); for (int i = 0; i < array.size(); i++) { cin >> array[i]; } for (int i = 0; i < array.size(); i++) { cout << array[i]; } //实现数组的连接 Array one(3); //输入1 2 3 cin >> one; Array two(3); //输入2 3 4 cin >> two; Array sum = one + two; cout << sum << endl; //打印1 2 3 2 3 4 Array num; num = sum; cout << num << endl; return 0; }
-
6.C++运算符重载
最新推荐文章于 2022-12-08 22:40:14 发布