博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操作符重载
阅读量:4472 次
发布时间:2019-06-08

本文共 5747 字,大约阅读时间需要 19 分钟。

操作符重载:

  在自定义的数据类型的数据间的加减乘除操作,默认情况下编译器是不识别的,需要进行操作重载,格式如operator+(param);

  但是并不是所有的运算符都是可以重载或有必要重载的!

  

 

重载规则:

        

        

 

实例代码:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include 
3 #include
4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) {10 this->a = a;11 this->b = b;12 }13 14 void print_complex() {15 cout << "(" << this->a << "," << this->b << "i)" << endl;16 }17 18 Complex complex_add(Complex &another) {19 Complex temp(this->a + another.a, this->b + another.b);20 return temp;21 }22 Complex operator+(Complex &another) {23 Complex temp(this->a + another.a, this->b + another.b);24 return temp;25 }26 27 friend Complex complex_add(Complex &c1, Complex &c2);28 //friend Complex operator+(Complex &c1, Complex &c2);29 private:30 int a;//实部31 int b;//虚部32 };33 34 //对象加法的全局函数35 Complex complex_add(Complex &c1, Complex &c2) {36 Complex temp(c1.a + c2.a, c1.b + c2.b);37 return temp;38 }39 40 #if 041 //操作符重载,全局42 Complex operator+(Complex &c1, Complex &c2) {43 Complex temp(c1.a + c2.a, c1.b + c2.b);44 return temp;45 }46 #endif47 48 int main(void) {49 Complex c1(1, 2);50 Complex c2(2, 4);51 52 c1.print_complex();53 c2.print_complex();54 55 //Complex c3 = complex_add(c1, c2);56 //Complex c3 = c1.complex_add(c2);57 58 //默认情况下,'+'对于自定义的数据类型编译器是不识别的59 //需要操作符重载60 Complex c3 = c1 + c2;//c1+c2会同时匹配全局的operator+(c1,c2)和局部的c1.operator+(c2)61 c3.print_complex();62 Complex c4 = c1 + c2 + c3;//可以满足连加63 c4.print_complex();64 system("pause");65 return 0;66 }
'+'运算符重载实例代码

 

  双目运算符'+='的重载:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include 
3 #include
4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) {10 this->a = a;11 this->b = b;12 }13 14 void print_complex() {15 cout << "(" << this->a << "," << this->b << "i)" << endl;16 }17 18 Complex & operator+=(Complex &another) {19 this->a += another.a;20 this->b += another.b;21 return *this;22 }23 24 //friend Complex & operator+=(Complex &c1, Complex &c2);25 26 private:27 int a;//实部28 int b;//虚部29 };30 31 32 #if 033 //操作符重载,全局34 Complex & operator+=(Complex &c1, Complex &c2) {35 c1.a += c2.a;36 c1.b += c2.b;37 return c1;38 }39 #endif40 41 int main(void) {42 Complex c1(1, 2);43 Complex c2(2, 4);44 45 c1.print_complex();46 c2.print_complex();47 48 (c1 += c2) += c2;49 c1.print_complex();50 system("pause");51 return 0;52 }
’+=’运算符的重载实例代码

 

  单目运算符'++'的重载:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include 
3 #include
4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) {10 this->a = a;11 this->b = b;12 }13 14 void print_complex() {15 cout << "(" << this->a << "," << this->b << "i)" << endl;16 }17 //前++18 Complex & operator++() {19 this->a++;20 this->b++;21 return *this;22 }23 //后++24 const Complex operator++(int) {
//int称为亚元25 Complex temp(this->a, this->b);26 this->a++;//***让当前对象的属性值++27 this->b++;28 return temp;//返回临时变量的值还是当前对象++之前的值,从而实现了先用后加的效果29 }30 //前++返回引用31 //friend Complex & operator++(Complex &c);32 33 //后++返回值不可以修改所以用了const修饰,占位符int只是为了区分后++34 //friend const Complex operator++(Complex &c, int);35 36 private:37 int a;//实部38 int b;//虚部39 };40 41 42 #if 043 //操作符前++重载,全局44 Complex & operator++(Complex &c) {45 c.a++;46 c.b++;47 return c;48 }49 #endif50 51 #if 052 //操作符后++重载,全局53 const Complex operator++(Complex &c,int) {54 Complex temp(c.a, c.b);55 c.a++;56 c.b++;57 return temp;//返回的是值,用中间变量的方式实现后++的先用后加的效果58 }59 #endif60 61 int main(void) {62 Complex c1(1, 2);63 Complex c2(2, 4);64 65 c1.print_complex();66 c2.print_complex();67 68 ++++c1;69 c1.print_complex();70 71 c2++;72 c2.print_complex();73 system("pause");74 return 0;75 }
’++’运算符的重载实例代码

   左移右移操作符重载:

   左移右移操作符的重载都必须写在类的外部,全局的,否则写在类内部会出现调用时顺序变反,如:c1<<cout。

1 #define _CRT_SECURE_NO_WARNINGS 2 #include 
3 #include
4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) {10 this->a = a;11 this->b = b;12 }13 14 void print_complex() {15 cout << "(" << this->a << "," << this->b << "i)" << endl;16 }17 18 friend ostream & operator<<(ostream &os,Complex &c);19 20 friend istream & operator>>(istream &is,Complex &c);21 22 private:23 int a;//实部24 int b;//虚部25 };26 27 28 #if 129 30 ostream & operator<<(ostream &os, Complex &c) {31 os << "(" << c.a << "," << c.b << "i)" << endl;32 return os;33 }34 istream & operator>>(istream &is, Complex &c) {35 cout << "a:";36 is >> c.a;37 cout << "b:";38 is >> c.b;39 return is;40 }41 #endif42 43 int main(void) {44 Complex c1(1, 2);45 46 cin >> c1;47 cout << c1;48 system("pause");49 return 0;50 }
左移右移运算符重载示例代码

   ’=‘操作符的重载,首先判断是不是自身赋值,然后将自身额外开辟的空间回收,最后再执行深拷贝。

  仿函数(functor)是把类对象像函数一样使用,其实就是一个类实现了operator(),使得类对象有了函数行为。

  ’()‘操作符重载,一般用于仿函数(伪函数)的使用,譬如给一个对象排序就需要()重载的仿函数实现。

            

//()重载,也可以传多个参数int operator()(int value1, int value2){    return value1*value2;}

  new和delete操作符也可以重载,有类A,其私有成员为 int a;

  new的重载使用C语言的malloc()函数,但这个重载仍然会出发对象的构造函数;

  delete的重载使用C语言的free()函数,但这个重载仍然也会触发对象的析构函数。

      

      

 

转载于:https://www.cnblogs.com/zzx1905/p/operator_reload.html

你可能感兴趣的文章
jQuery实战之仿淘宝商城左侧导航效果
查看>>
AC日记——「SCOI2016」幸运数字 LiBreOJ 2013
查看>>
unmount
查看>>
数据库连接池
查看>>
javascript获得和设置以及移除元素属性的三个方法
查看>>
windwos iis 7.5 使用html 报405错误
查看>>
Java面向对象学习1——概念等
查看>>
redis集群搭建(简单简单)一台机器多redis
查看>>
【生活篇】微信运动刷步,高达98000!微信运动计步作弊教程!
查看>>
JAVA中获取工程路径的方法
查看>>
执行命令类
查看>>
hbase SingleColumnValueFilter 列不存在 无法过滤
查看>>
第一天
查看>>
linux ls -l 详解
查看>>
48、C++ Primer 4th 笔记,句柄类,继承,虚函数等的一个综合例子(未完)
查看>>
Tomcat搭建&配置
查看>>
2015535俞昆《网络对抗技术》恶意代码分析
查看>>
四种以太网数据包详解
查看>>
hrbust-1545-基础数据结构——顺序表(2)
查看>>
windows服务-log4net的使用
查看>>