您的位置:首页 > 编程语言 > C语言/C++

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

2015-09-24 22:26 615 查看
1. 操作符(++,+,+=,小于号等)重载

新建QT项目,编写头文件

[cpp] view
plaincopyprint?

#ifndef DIALOG_H

#define DIALOG_H



#include <QDialog>

#include<QLabel>



namespace Ui {

class Dialog;

}



//编写自己的Label

class 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_OBJECT



public:

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

编写头文件的实现类

[cpp] view plaincopyprint?#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);  
}


编写主函数

[cpp] view plaincopyprint?#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();  
}  
  
//>  
//=  
//+=  
//友元重载+=,=不同类的对象的位置,因为这里的泛型是类,所以下面使用class  
template<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.重载--,()操作符的代码:

头文件:


[cpp] view
plaincopyprint?

#ifndef DIALOG_H

#define DIALOG_H



#include <QDialog>



namespace Ui {

class Dialog;

}



class Dialog : public QDialog

{

Q_OBJECT



public:

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

头文件的实现类


[cpp] view
plaincopyprint?

#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;

}

主函数所在类


[cpp] view
plaincopyprint?

#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.通过友元的方式重载操作<<和>>还有+操作符

[cpp] view
plaincopyprint?

#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();

}

运行结果:




[cpp] view
plaincopyprint?

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();

}

运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: