概述:
1.成员函数方式重载加法运算符;
2.全局函数重载加法操作运算符;
3.全局函数重载加法操作运算符(自定义类型和int);
示例代码:
.h(加法运算符重载类)
#ifndef OPERATORADD_H
#define OPERATORADD_H
/**
** time : 2020/1/3 PM21:41
** author : Fantast
** declare: 加法运算符重载类
** 1.成员函数方式重载加法运算操作符
** 2.全局函数方式重载加法运算操作符
** 3.函数重载加法运算操作符
**/
#include <QObject>
class OperatorAdd
{
public:
OperatorAdd();
~OperatorAdd();
public:
/**
* @brief operator + 加法运算符重载(1.成员函数方式重载加法运算符)
* @param oper
* @return
*/
OperatorAdd operator+(const OperatorAdd &oper);
public:
int m_A;
int m_B;
};
/**
* @brief operator + 全局函数重载加法操作运算符
* @param p1
* @param p2
* @return
*/
OperatorAdd operator+(OperatorAdd &p1,OperatorAdd &p2);
OperatorAdd operator+(const OperatorAdd &oper,const int &num);
#endif // OPERATORADD_H
.cpp
#include "OperatorAdd.h"
OperatorAdd::OperatorAdd()
{
}
OperatorAdd::~OperatorAdd()
{
}
//成员函数
OperatorAdd OperatorAdd::operator+(const OperatorAdd &oper)
{
OperatorAdd add;
add.m_A = this->m_A + oper.m_A;
add.m_B = this->m_B + oper.m_B;
return add;
}
//全局函数
OperatorAdd operator+(OperatorAdd &p1, OperatorAdd &p2)
{
OperatorAdd add;
add.m_A = p1.m_A + p2.m_A;
add.m_B = p1.m_B + p2.m_B;
return add;
}
OperatorAdd operator+(const OperatorAdd &oper, const int &num)
{
OperatorAdd add;
add.m_A = oper.m_A + num;
add.m_B = oper.m_B + num;
return add;
}
使用类:
.h
#ifndef OPERATOR_H
#define OPERATOR_H
#include <QWidget>
#include <QDebug>
namespace Ui {
class Operator;
}
class Operator : public QWidget
{
Q_OBJECT
public:
explicit Operator(QWidget *parent = 0);
~Operator();
private slots:
void on_pushButton_Add1_clicked();
void on_pushButton_clicked();
void on_pushButton_Add2_clicked();
private:
void OperatorAddTestMemberFunc();
void OperatorAddTestSituationFunc();
void OperatorAddTestMemberIntFunc();
private:
//把数据加载到界面
void setDataToUi(const QString &text1,const QString &text2);
private:
Ui::Operator *ui;
};
#endif // OPERATOR_H
.cpp
#include "Operator.h"
#include "ui_Operator.h"
#include "OperatorAdd.h"
Operator::Operator(QWidget *parent) :
QWidget(parent),
ui(new Ui::Operator)
{
ui->setupUi(this);
}
Operator::~Operator()
{
delete ui;
}
void Operator::OperatorAddTestMemberFunc()
{
OperatorAdd add1;
add1.m_A = 10;
add1.m_B = 10;
OperatorAdd add2;
add2.m_A = 20;
add2.m_B = 20;
OperatorAdd add3 = add1 + add2;
//成员函数本质调用(同上)
//OperatorAdd add3 = add1.operator +(add2);
setDataToUi(QString::number(add3.m_A,10),QString::number(add3.m_B,10));
}
void Operator::OperatorAddTestSituationFunc()
{
OperatorAdd add1;
add1.m_A = 20;
add1.m_B = 20;
OperatorAdd add2;
add2.m_A = 20;
add2.m_B = 20;
OperatorAdd add3 = add1 + add2;
//全局函数本质调用(同上)
//OperatorAdd add3 = operator +(add1,add2);
setDataToUi(QString::number(add3.m_A,10),QString::number(add3.m_B,10));
}
void Operator::OperatorAddTestMemberIntFunc()
{
OperatorAdd add1;
add1.m_A = 20;
add1.m_B = 20;
OperatorAdd add3 = add1 + 110;
//全局函数本质调用(同上)
//OperatorAdd add3 = operator +(add1,add2);
setDataToUi(QString::number(add3.m_A,10),QString::number(add3.m_B,10));
}
void Operator::setDataToUi(const QString &text1, const QString &text2)
{
ui->textBrowser->append(QString("%1-%2").arg(text1).arg(text2));
}
void Operator::on_pushButton_Add1_clicked()
{
OperatorAddTestMemberFunc();
}
void Operator::on_pushButton_clicked()
{
OperatorAddTestMemberIntFunc();
}
void Operator::on_pushButton_Add2_clicked()
{
OperatorAddTestSituationFunc();
}
over:
欢迎大家关注作者在文末评论、点赞、转发以及批评指正!
如果大家有更好的方法或有问题可以在文末评论一起讨论!
共同学习!
共同进步!
文末一句话:
我昊哥说了:人间正道是沧桑!
铁子努力吧!要不然吃羊肉串都得闻味味啊!