日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

操作符(++,+,+=,小于号,(),--等)重载

發(fā)布時間:2024/9/27 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作符(++,+,+=,小于号,(),--等)重载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


1. 操作符(+++,+=,小于號等)重載

新建QT項目,編寫頭文件

#ifndef DIALOG_H #define DIALOG_H#include <QDialog> #include<QLabel>namespace Ui { class Dialog; }//編寫自己的Label class myLabel { public: //一定要是共有的,才可以被調(diào)用QLabel *ql;int cx;int cy;myLabel(){ql=new QLabel("12345") ;cx=cy=300;ql->move(cx,cy);//移動位置}~myLabel(){delete ql;}void show(){ql->show();} };class Dialog : public QDialog {Q_OBJECTpublic:int x;//長,寬int y;int cx;//移動到的位置x坐標(biāo)int cy;public:explicit Dialog(QWidget *parent = 0);//通過友元函數(shù)進(jìn)行重載friend void operator +=(Dialog &d,myLabel &my);~Dialog();//重載右加號void operator ++();void setxy();void settoxy(int a,int b);//二元運(yùn)算符,重載+號Dialog * operator +(Dialog const &adddata);//重載等號Dialog *operator =(Dialog const &setdata);//重載+=Dialog *operator +=(int length);//重載<bool operator < (Dialog const &data);private:Ui::Dialog *ui; };#endif // DIALOG_H

編寫頭文件的實現(xiàn)類

#include "dialog.h" #include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog) {ui->setupUi(this);x = y = 300;//將窗口大小固定this->resize(x,y);cx = cy = 0;this->move(cx,cy); }Dialog::~Dialog() {delete ui; }//重載左加號,語法:括號里面有int,默認(rèn)后++,沒有int,默認(rèn)前-- void Dialog::operator ++() {this->cx++;this->cy++;this->move(cx,cy); }void Dialog::setxy() {this->resize(x,y); } void Dialog::settoxy(int a,int b) {this->x = a;this->y = b; }//二元運(yùn)算符,重載+號 Dialog * Dialog::operator +(Dialog const &adddata) {Dialog *p=new Dialog;p->x=this->x+adddata.x;p->y=this->y+adddata.y;return p; }//重載等號 Dialog * Dialog::operator =(Dialog const &setdata) {this->x = setdata.x;this->y = setdata.y;this->setxy();return this; }//重載+=,如果+號前的變量和定義的變量類型一致,只需要一個參數(shù) Dialog * Dialog::operator +=(int length) {this->x+= length;this->y+= length;this->setxy();return this; }//重載< bool Dialog::operator < (Dialog const &data) {return (this->x * this->y) < (data.x * data.y); }

編寫主函數(shù)

#include "dialog.h" #include <QApplication> #include <windows.h> #include <QDebug>//在棧上定義一個臨時的Dialog,顯示,然后休眠2秒鐘后消失 void add(Dialog &add1,Dialog &add2) {Dialog temp; //棧上,用完了馬上回收temp.x = add1.x + add2.x;temp.y = add1.y + add2.y;temp.show();temp.setxy();//發(fā)現(xiàn)這里在停留2秒鐘后,窗口消失了。Sleep(2000); }//在堆上,只有自己回收才消失 Dialog * addX(Dialog &add1,Dialog &add2) {//在堆上創(chuàng)建一個Dialog,只有當(dāng)自己回收了的時候才會被銷毀Dialog *p = new Dialog;p->x = add1.x + add2.x;p->y = add1.y + add2.y;p->show();p->setxy();return p; }//測試方法的方式實現(xiàn)+操作 //1.(分為兩種情況:1.棧上,2,堆上) //2.通過重載+操作符的方式實現(xiàn) int main1(int argc, char *argv[]) {QApplication a(argc, argv);Dialog w1;//不管最終怎樣,因為這里使用了w1.show()所以永遠(yuǎn)有下面窗口w1.show();Dialog w2;//不管最終怎樣,因為這里使用了w1.show()所以永遠(yuǎn)有下面窗口w2.show();add(w1,w2);//下面開始使用在堆上創(chuàng)建窗口的方式演示效果,最后Dialog *p = addX(w1,w2);//顯示窗口p->show();//下面通過重載+號的方式顯示窗口Dialog *p2 = w1 + w2;p2->setxy();p2->show();return a.exec(); }//重載右++,因為右邊沒有參數(shù),所以就不需要帶參數(shù) int main2(int argc,char *argv[]) {QApplication a(argc,argv);Dialog w;w.show();//將窗口先移動到(0,0)位置w.move(0,0);for(int i=0;i<400;i++){//重載左加號,語法:括號里面有int,默認(rèn)后++,沒有int,默認(rèn)前--++w;Sleep(5);}return a.exec(); }//> //= //+= //友元重載+=,=不同類的對象的位置,因為這里的泛型是類,所以下面使用class template<class T> void showall(T* myt) {myt->show(); } //測試上面的友元函數(shù),測試重載函數(shù),+=重載 //友元函數(shù)的好處:訪問私有變量 //如果重載的時候,用到私有變量的時候,不需要友元 int main3(int argc,char *argv[]) {QApplication a(argc,argv);//調(diào)用默認(rèn)的Label,如果只寫下面這兩句,也會顯示一個帶有Label的小窗口QLabel *ql = new QLabel("1234567");ql->show();myLabel label1;showall(&label1);//通過模板的方式實現(xiàn)顯示label//再定義一個Label,驗證上面的重載函數(shù)Dialog w1;//下面?zhèn)鬟f一個指針showall(&w1);return a.exec(); }//測試=,+=,<操作符 int main(int argc,char *argv[]) {QApplication a(argc,argv);Dialog w1;w1.setWindowTitle("w1窗口");w1.show();w1.settoxy(400,700);w1.setxy();Dialog w2;w2.setWindowTitle("w2窗口");w2.show();w2.settoxy(700,400);w2.setxy();//使用重載的=號操作符,運(yùn)行效果是窗口2的大小和窗口1的大小一樣了//如果下面的這行注釋掉,那么就發(fā)現(xiàn)兩個窗口大小變成一樣了。w1=w2;w2.show();Dialog w3;w3.setWindowTitle("w3窗口");w3.show();w3.settoxy(300,1050);w3.setxy();w3+=230;w3.show();//<重載的是面積比大小qDebug() << (w1 < w2);qDebug() << (w2 < w3);return a.exec(); }
2.重載--,()操作符的代碼: 頭文件:

#ifndef DIALOG_H #define DIALOG_H#include <QDialog>namespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECTpublic:int x;int y;//重載++操作符void operator ++();//重載--操作符void operator --();//重載()void operator ()(int num);//重載+操作符,創(chuàng)建窗口的時候在堆上Dialog * operator +(Dialog & adddata2);void setxy(int a,int b){this->x = a;this->y = b;this->resize(this->x,this->y);}public:explicit Dialog(QWidget *parent = 0);~Dialog();private:Ui::Dialog *ui; };#endif // DIALOG_H

頭文件的實現(xiàn)類

#include "dialog.h" #include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog) {ui->setupUi(this);this->x = 300;this->y = 400;this->resize(x,y); }Dialog::~Dialog() {delete ui; }//重載++操作符 void Dialog::operator ++() {this->x++;this->y++;this->resize(x,y); }//重載--操作符 void Dialog::operator --() {this->x--;this->y--;this->resize(x,y); }//重載() void Dialog::operator ()(int num) {this->x = num;this->y = num / 2;this->resize(x,y); } //重載+操作符,創(chuàng)建窗口的時候在堆上 Dialog * Dialog::operator +(Dialog & adddata2) {Dialog *p=new Dialog;p->x=this->x+adddata2.x;p->y=this->x+adddata2.y;p->resize(p->x,p->y);p->show();return p; }

主函數(shù)所在類

#include "dialog.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Dialog w;w.show();for(int i = 0;i<800;i++){++w;}for(int i=800;i>0;i++){--w;}for(int i = 1000;i>300;i--){w(i);}return a.exec(); }

3.通過友元的方式重載操作<<>>還有+操作符

#include<iostream> using namespace std;class mycomplex { public://友元,需要操作類的內(nèi)部//ostream,引用標(biāo)準(zhǔn)輸入輸出流friend ostream & operator <<(ostream & out, mycomplex & Complex);friend istream & operator >>(istream & in, mycomplex & Complex);friend mycomplex operator +(mycomplex adddata1, mycomplex adddata2);friend mycomplex operator +(mycomplex adddata1, int x);//友元函數(shù)可以處理不同的類型交錯//成員函數(shù)不能實現(xiàn)的,友元函數(shù)都可以實現(xiàn)int x;int y; //x,y坐標(biāo)//沒有構(gòu)造無法使用this初始化mycomplex(){this->x = 0;this->y = 0;}//構(gòu)造函數(shù)重載mycomplex(int x, int y) :x(x), y(y){}~mycomplex(){}void show(){std::cout << x << "+" << y << "i" << std::endl;}//重載++操作符void operator ++(){this->x++;this->y++;}//重載--操作符void operator --();//重載函數(shù)調(diào)用運(yùn)算符,變量名可以當(dāng)作一個函數(shù)調(diào)用參數(shù),返回結(jié)果int operator()(int num){cout << num << endl;return num + num;} protected: private: };void mycomplex::operator--() {this->x--;this->y--; }//輸入輸出,cout,屏幕,fout文件 ostream & operator <<(ostream & out, mycomplex & Complex) {out << Complex.x << "+" << Complex.y << "i" << std::endl;return out; }istream & operator >>(istream & in, mycomplex & Complex) {cout << "請輸入X,Y" << endl;in >> Complex.x >> Complex.y;return in; }mycomplex operator +(mycomplex adddata1, mycomplex adddata2) {mycomplex temp;temp.x = adddata1.x + adddata2.x;temp.y = adddata1.y + adddata2.y;return temp; }mycomplex operator + (mycomplex adddata1, int x) {mycomplex temp;temp.x = adddata1.x + x;temp.y = adddata1.y + x;return temp; }void main() {mycomplex my1(7,8),my2(9,10);//my1+my2這里+實現(xiàn)了重載功能,返回的是一個mycomplex對象,其中<<又被重載了std::cout << my1 + my2 << std::endl;//my1+3這里+實現(xiàn)了重載功能,返回的是一個mycomplex對象,其中<<又被重載了std::cout << my1 + 3 << std::endl;std::cin.get(); }

運(yùn)行結(jié)果:

?

void main() {mycomplex my1;//>>調(diào)用了重載cin >> my1;cout << my1;//my1++;//左加加,括號中不需要帶參數(shù)++my1;cout << my1;my1--;cout << my1;//下面每行執(zhí)行的時候分別輸出了2行數(shù)據(jù)。std::cout << my1(1) << std::endl;std::cout << my1(2) << std::endl;std::cin.get();std::cin.get(); }

運(yùn)行結(jié)果如下:

??

總結(jié)

以上是生活随笔為你收集整理的操作符(++,+,+=,小于号,(),--等)重载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。