C/C++ 之 ‘++’、‘+’、‘+=’、‘-’、‘()’、‘--’ 操作符的重载实现重载

末蓝、 2022-08-08 19:36 240阅读 0赞
  1. 操作符(++,+,+=,小于号等)重载

新建QT项目,编写头文件

[cpp] view plain copy print ?

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include
  4. #include
  5. namespace Ui {
  6. class Dialog;
  7. }
  8. //编写自己的Label
  9. class myLabel
  10. {
  11. public: //一定要是共有的,才可以被调用
  12. QLabel *ql;
  13. int cx;
  14. int cy;
  15. myLabel()
  16. {
  17. ql=new QLabel(“12345”) ;
  18. cx=cy=300;
  19. ql->move(cx,cy);//移动位置
  20. }
  21. ~myLabel()
  22. {
  23. delete ql;
  24. }
  25. void show()
  26. {
  27. ql->show();
  28. }
  29. };
  30. class Dialog : public QDialog
  31. {
  32. Q_OBJECT
  33. public:
  34. int x;//长,宽
  35. int y;
  36. int cx;//移动到的位置x坐标
  37. int cy;
  38. public:
  39. explicit Dialog(QWidget *parent = 0);
  40. //通过友元函数进行重载
  41. friend**void** operator +=(Dialog &d,myLabel &my);
  42. ~Dialog();
  43. //重载右加号
  44. void operator ++();
  45. void setxy();
  46. void settoxy(int a,int b);
  47. //二元运算符,重载+号
  48. Dialog * operator +(Dialog const &adddata);
  49. //重载等号
  50. Dialog *operator =(Dialog const &setdata);
  51. //重载+=
  52. Dialog *operator +=(int length);
  53. //重载<
  54. bool operator < (Dialog const &data);
  55. private:
  56. Ui::Dialog *ui;
  57. };
  58. #endif // DIALOG_H

编写头文件的实现类

  1. [cpp]
  2. view plain
  3. copy
  4. print
  5. ?
  6. #include "dialog.h"
  7. #include "ui_dialog.h"
  8. Dialog::Dialog(QWidget *parent) :
  9. QDialog(parent),
  10. ui(new Ui::Dialog)
  11. {
  12. ui->setupUi(this);
  13. x = y = 300;
  14. //将窗口大小固定
  15. this->resize(x,y);
  16. cx = cy = 0;
  17. this->move(cx,cy);
  18. }
  19. Dialog::~Dialog()
  20. {
  21. delete ui;
  22. }
  23. //重载左加号,语法:括号里面有int,默认后++,没有int,默认前--
  24. void Dialog::operator ++()
  25. {
  26. this->cx++;
  27. this->cy++;
  28. this->move(cx,cy);
  29. }
  30. void Dialog::setxy()
  31. {
  32. this->resize(x,y);
  33. }
  34. void Dialog::settoxy(int a,int b)
  35. {
  36. this->x = a;
  37. this->y = b;
  38. }
  39. //二元运算符,重载+号
  40. Dialog * Dialog::operator +(Dialog const &adddata)
  41. {
  42. Dialog *p=new Dialog;
  43. p->x=this->x+adddata.x;
  44. p->y=this->y+adddata.y;
  45. return p;
  46. }
  47. //重载等号
  48. Dialog * Dialog::operator =(Dialog const &setdata)
  49. {
  50. this->x = setdata.x;
  51. this->y = setdata.y;
  52. this->setxy();
  53. return this;
  54. }
  55. //重载+=,如果+号前的变量和定义的变量类型一致,只需要一个参数
  56. Dialog * Dialog::operator +=(int length)
  57. {
  58. this->x+= length;
  59. this->y+= length;
  60. this->setxy();
  61. return this;
  62. }
  63. //重载<
  64. bool Dialog::operator < (Dialog const &data)
  65. {
  66. return (this->x * this->y) < (data.x * data.y);
  67. }

编写主函数

  1. [cpp]
  2. view plain
  3. copy
  4. print
  5. ?
  6. #include "dialog.h"
  7. #include <QApplication>
  8. #include <windows.h>
  9. #include <QDebug>
  10. //在栈上定义一个临时的Dialog,显示,然后休眠2秒钟后消失
  11. void add(Dialog &add1,Dialog &add2)
  12. {
  13. Dialog temp; //栈上,用完了马上回收
  14. temp.x = add1.x + add2.x;
  15. temp.y = add1.y + add2.y;
  16. temp.show();
  17. temp.setxy();
  18. //发现这里在停留2秒钟后,窗口消失了。
  19. Sleep(2000);
  20. }
  21. //在堆上,只有自己回收才消失
  22. Dialog * addX(Dialog &add1,Dialog &add2)
  23. {
  24. //在堆上创建一个Dialog,只有当自己回收了的时候才会被销毁
  25. Dialog *p = new Dialog;
  26. p->x = add1.x + add2.x;
  27. p->y = add1.y + add2.y;
  28. p->show();
  29. p->setxy();
  30. return p;
  31. }
  32. //测试方法的方式实现+操作
  33. //1.(分为两种情况:1.栈上,2,堆上)
  34. //2.通过重载+操作符的方式实现
  35. int main1(int argc, char *argv[])
  36. {
  37. QApplication a(argc, argv);
  38. Dialog w1;
  39. //不管最终怎样,因为这里使用了w1.show()所以永远有下面窗口
  40. w1.show();
  41. Dialog w2;
  42. //不管最终怎样,因为这里使用了w1.show()所以永远有下面窗口
  43. w2.show();
  44. add(w1,w2);
  45. //下面开始使用在堆上创建窗口的方式演示效果,最后
  46. Dialog *p = addX(w1,w2);
  47. //显示窗口
  48. p->show();
  49. //下面通过重载+号的方式显示窗口
  50. Dialog *p2 = w1 + w2;
  51. p2->setxy();
  52. p2->show();
  53. return a.exec();
  54. }
  55. //重载右++,因为右边没有参数,所以就不需要带参数
  56. int main2(int argc,char *argv[])
  57. {
  58. QApplication a(argc,argv);
  59. Dialog w;
  60. w.show();
  61. //将窗口先移动到(0,0)位置
  62. w.move(0,0);
  63. for(int i=0;i<400;i++)
  64. {
  65. //重载左加号,语法:括号里面有int,默认后++,没有int,默认前--
  66. ++w;
  67. Sleep(5);
  68. }
  69. return a.exec();
  70. }
  71. //>
  72. //=
  73. //+=
  74. //友元重载+=,=不同类的对象的位置,因为这里的泛型是类,所以下面使用class
  75. template<class T>
  76. void showall(T* myt)
  77. {
  78. myt->show();
  79. }
  80. //测试上面的友元函数,测试重载函数,+=重载
  81. //友元函数的好处:访问私有变量
  82. //如果重载的时候,用到私有变量的时候,不需要友元
  83. int main3(int argc,char *argv[])
  84. {
  85. QApplication a(argc,argv);
  86. //调用默认的Label,如果只写下面这两句,也会显示一个带有Label的小窗口
  87. QLabel *ql = new QLabel("1234567");
  88. ql->show();
  89. myLabel label1;
  90. showall(&label1);//通过模板的方式实现显示label
  91. //再定义一个Label,验证上面的重载函数
  92. Dialog w1;
  93. //下面传递一个指针
  94. showall(&w1);
  95. return a.exec();
  96. }
  97. //测试=,+=,<操作符
  98. int main(int argc,char *argv[])
  99. {
  100. QApplication a(argc,argv);
  101. Dialog w1;
  102. w1.setWindowTitle("w1窗口");
  103. w1.show();
  104. w1.settoxy(400,700);
  105. w1.setxy();
  106. Dialog w2;
  107. w2.setWindowTitle("w2窗口");
  108. w2.show();
  109. w2.settoxy(700,400);
  110. w2.setxy();
  111. //使用重载的=号操作符,运行效果是窗口2的大小和窗口1的大小一样了
  112. //如果下面的这行注释掉,那么就发现两个窗口大小变成一样了。
  113. w1=w2;
  114. w2.show();
  115. Dialog w3;
  116. w3.setWindowTitle("w3窗口");
  117. w3.show();
  118. w3.settoxy(300,1050);
  119. w3.setxy();
  120. w3+=230;
  121. w3.show();
  122. //<重载的是面积比大小
  123. qDebug() << (w1 < w2);
  124. qDebug() << (w2 < w3);
  125. return a.exec();
  126. }
  127. 2.重载--,()操作符的代码:
  128. 头文件:

[cpp] view plain copy print ?

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include
  4. namespace Ui {
  5. class Dialog;
  6. }
  7. class Dialog : public QDialog
  8. {
  9. Q_OBJECT
  10. public:
  11. int x;
  12. int y;
  13. //重载++操作符
  14. void operator ++();
  15. //重载—操作符
  16. void operator —();
  17. //重载()
  18. void operator ()(int num);
  19. //重载+操作符,创建窗口的时候在堆上
  20. Dialog * operator +(Dialog & adddata2);
  21. void setxy(int a,int b)
  22. {
  23. this->x = a;
  24. this->y = b;
  25. this->resize(this->x,this->y);
  26. }
  27. public:
  28. explicit Dialog(QWidget *parent = 0);
  29. ~Dialog();
  30. private:
  31. Ui::Dialog *ui;
  32. };
  33. #endif // DIALOG_H
  1. 头文件的实现类

[cpp] view plain copy print ?

  1. #include “dialog.h”
  2. #include “ui_dialog.h”
  3. Dialog::Dialog(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::Dialog)
  6. {
  7. ui->setupUi(this);
  8. this->x = 300;
  9. this->y = 400;
  10. this->resize(x,y);
  11. }
  12. Dialog::~Dialog()
  13. {
  14. delete ui;
  15. }
  16. //重载++操作符
  17. void Dialog::operator ++()
  18. {
  19. this->x++;
  20. this->y++;
  21. this->resize(x,y);
  22. }
  23. //重载—操作符
  24. void Dialog::operator —()
  25. {
  26. this->x—;
  27. this->y—;
  28. this->resize(x,y);
  29. }
  30. //重载()
  31. void Dialog::operator ()(int num)
  32. {
  33. this->x = num;
  34. this->y = num / 2;
  35. this->resize(x,y);
  36. }
  37. //重载+操作符,创建窗口的时候在堆上
  38. Dialog * Dialog::operator +(Dialog & adddata2)
  39. {
  40. Dialog *p=new Dialog;
  41. p->x=this->x+adddata2.x;
  42. p->y=this->x+adddata2.y;
  43. p->resize(p->x,p->y);
  44. p->show();
  45. return p;
  46. }
  1. 主函数所在类

[cpp] view plain copy print ?

  1. #include “dialog.h”
  2. #include
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Dialog w;
  7. w.show();
  8. for(int i = 0;i<800;i++)
  9. {
  10. ++w;
  11. }
  12. for(int i=800;i>0;i++)
  13. {
  14. --w;
  15. }
  16. for(int i = 1000;i>300;i—)
  17. {
  18. w(i);
  19. }
  20. return a.exec();
  21. }

3.通过友元的方式重载操作<<和>>还有+操作符

[cpp] view plain copy print ?

  1. #include
  2. using**namespace** std;
  3. class mycomplex
  4. {
  5. public:
  6. //友元,需要操作类的内部
  7. //ostream,引用标准输入输出流
  8. friend ostream & operator <<(ostream & out, mycomplex & Complex);
  9. friend istream & operator >>(istream & in, mycomplex & Complex);
  10. friend mycomplex operator +(mycomplex adddata1, mycomplex adddata2);
  11. friend mycomplex operator +(mycomplex adddata1, int x);
  12. //友元函数可以处理不同的类型交错
  13. //成员函数不能实现的,友元函数都可以实现
  14. int x;
  15. int y; //x,y坐标
  16. //没有构造无法使用this初始化
  17. mycomplex()
  18. {
  19. this->x = 0;
  20. this->y = 0;
  21. }
  22. //构造函数重载
  23. mycomplex(int x, int y) :x(x), y(y)
  24. {
  25. }
  26. ~mycomplex()
  27. {
  28. }
  29. void show()
  30. {
  31. std::cout << x << “+” << y << “i” << std::endl;
  32. }
  33. //重载++操作符
  34. void operator ++()
  35. {
  36. this->x++;
  37. this->y++;
  38. }
  39. //重载—操作符
  40. void operator —();
  41. //重载函数调用运算符,变量名可以当作一个函数调用参数,返回结果
  42. int operator()(int num)
  43. {
  44. cout << num << endl;
  45. return num + num;
  46. }
  47. protected:
  48. private:
  49. };
  50. void mycomplex::operator—()
  51. {
  52. this->x—;
  53. this->y—;
  54. }
  55. //输入输出,cout,屏幕,fout文件
  56. ostream & operator <<(ostream & out, mycomplex & Complex)
  57. {
  58. out << Complex.x << “+” << Complex.y << “i” << std::endl;
  59. return out;
  60. }
  61. istream & operator >>(istream & in, mycomplex & Complex)
  62. {
  63. cout << “请输入X,Y” << endl;
  64. in >> Complex.x >> Complex.y;
  65. return in;
  66. }
  67. mycomplex operator +(mycomplex adddata1, mycomplex adddata2)
  68. {
  69. mycomplex temp;
  70. temp.x = adddata1.x + adddata2.x;
  71. temp.y = adddata1.y + adddata2.y;
  72. return temp;
  73. }
  74. mycomplex operator + (mycomplex adddata1, int x)
  75. {
  76. mycomplex temp;
  77. temp.x = adddata1.x + x;
  78. temp.y = adddata1.y + x;
  79. return temp;
  80. }
  81. void main()
  82. {
  83. mycomplex my1(7,8),my2(9,10);
  84. //my1+my2这里+实现了重载功能,返回的是一个mycomplex对象,其中<<又被重载了
  85. std::cout << my1 + my2 << std::endl;
  86. //my1+3这里+实现了重载功能,返回的是一个mycomplex对象,其中<<又被重载了
  87. std::cout << my1 + 3 << std::endl;
  88. std::cin.get();
  89. }

运行结果:

SouthEast

[cpp] view plain copy print ?

  1. void main()
  2. {
  3. mycomplex my1;
  4. //>>调用了重载
  5. cin >> my1;
  6. cout << my1;
  7. //my1++;
  8. //左加加,括号中不需要带参数
  9. ++my1;
  10. cout << my1;
  11. my1—;
  12. cout << my1;
  13. //下面每行执行的时候分别输出了2行数据。
  14. std::cout << my1(1) << std::endl;
  15. std::cout << my1(2) << std::endl;
  16. std::cin.get();
  17. std::cin.get();
  18. }

运行结果如下:

SouthEast 1

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++操作符重载

    又看了一遍操作符的东西,感觉之前对操作符的理解还停留在很浅的认知上(仅仅会用哈哈),所以做一下笔记来加深一下印象。 文章目录 一、为什么会有操作符重载

    相关 C++中操作符重载

    一、操作符重载属于什么? 在本文开始阐述之前,首先明确,操作符重载是一个具有特殊名的“ 函数 ”。 既然作为函数,那么,就具有函数的特性,一是形参表,二是返回值。 关键字