操作符(++,+,+=,小於符號,(),--等)重載

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   strong   



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

建立QT項目,編寫標頭檔

#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include<QLabel>namespace Ui {class Dialog;}//編寫自己的Labelclass myLabel{public: //一定要是共有的,才可以被調用    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座標    int cy;public:    explicit Dialog(QWidget *parent = 0);    //通過友元函數進行重載    friend void operator +=(Dialog &d,myLabel &my);    ~Dialog();    //重載右加號    void operator ++();    void setxy();    void settoxy(int a,int b);    //二元運算子,重載+號    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

編寫標頭檔的實作類別

#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,預設後++,沒有int,預設前--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;}//二元運算子,重載+號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;}//重載+=,如果+號前的變數和定義的變數類型一致,只需要一個參數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);}

編寫主函數

#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();    //發現這裡在停留2秒鐘後,視窗消失了。    Sleep(2000);}//在堆上,只有自己回收才消失Dialog * addX(Dialog &add1,Dialog &add2){    //在堆上建立一個Dialog,只有當自己回收了的時候才會被銷毀    Dialog *p = new Dialog;    p->x = add1.x + add2.x;    p->y = add1.y + add2.y;    p->show();    p->setxy();    return p;}//測試方法的方式實現+操作//1.(分為兩種情況:1.棧上,2,堆上)//2.通過重載+操作符的方式實現int main1(int argc, char *argv[]){    QApplication a(argc, argv);    Dialog w1;    //不管最終怎樣,因為這裡使用了w1.show()所以永遠有下面視窗    w1.show();    Dialog w2;    //不管最終怎樣,因為這裡使用了w1.show()所以永遠有下面視窗    w2.show();    add(w1,w2);    //下面開始使用在堆上建立視窗的方式示範效果,最後    Dialog *p = addX(w1,w2);    //顯示視窗    p->show();    //下面通過重載+號的方式顯示視窗    Dialog *p2 = w1 + w2;    p2->setxy();    p2->show();    return a.exec();}//重載右++,因為右邊沒有參數,所以就不需要帶參數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,預設後++,沒有int,預設前--        ++w;        Sleep(5);    }    return a.exec();}//>//=//+=//友元重載+=,=不同類的對象的位置,因為這裡的泛型是類,所以下面使用classtemplate<class T>void showall(T* myt){    myt->show();}//測試上面的友元函數,測試重載函數,+=重載//友元函數的好處:訪問私人變數//如果重載的時候,用到私人變數的時候,不需要友元int main3(int argc,char *argv[]){    QApplication a(argc,argv);    //調用預設的Label,如果唯寫下面這兩句,也會顯示一個帶有Label的小視窗    QLabel *ql = new QLabel("1234567");    ql->show();    myLabel label1;    showall(&label1);//通過模板的方式實現顯示label    //再定義一個Label,驗證上面的重載函數    Dialog w1;    //下面傳遞一個指標    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();    //使用重載的=號操作符,運行效果是視窗2的大小和視窗1的大小一樣了    //如果下面的這行注釋掉,那麼就發現兩個視窗大小變成一樣了。    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);    //重載+操作符,建立視窗的時候在堆上    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

標頭檔的實作類別

#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);}//重載+操作符,建立視窗的時候在堆上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;}

主函數所在類

#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://友元,需要操作類的內部//ostream,引用標準輸入輸出資料流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);//友元函數可以處理不同的類型交錯//成員函數不能實現的,友元函數都可以實現int x;int y;   //x,y座標//沒有構造無法使用this初始化mycomplex(){this->x = 0;this->y = 0;}//建構函式重載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 --();//重載函數調用運算子,變數名可以當作一個函數調用參數,返回結果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這裡+實現了重載功能,返回的是一個mycomplex對象,其中<<又被重載了std::cout << my1 + my2 << std::endl;//my1+3這裡+實現了重載功能,返回的是一個mycomplex對象,其中<<又被重載了std::cout << my1 + 3 << std::endl;std::cin.get();}

運行結果:

 

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

運行結果如下:

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.