【C++】运算符重载

运算符重载示例。

TODO: 赋值运算符暂时存在问题。

头文件

/**
 * OperatorLoading.h
 */
#ifndef OPERATOROVERLOADING_MYINTEGER_H
#define OPERATOROVERLOADING_MYINTEGER_H

# include <iostream>

class MyInteger {
    // 以下全局函数实现的重载亦可仿照赋值运算符重载通过成员函数实现
    // 加法运算符重载, 减乘除同理
    friend MyInteger operator+(const MyInteger &min1, const MyInteger &min2);
    friend MyInteger operator+(const MyInteger &mint, int val);
    friend MyInteger operator+(int val, const MyInteger &mint);
    // 左移运算符重载
    friend std::ostream& operator<<(std::ostream &out, const MyInteger &mint);
    // 自增运算符重载, 前置&后置
    friend MyInteger& operator++(MyInteger &mint);
    friend MyInteger operator++(MyInteger &mint, int);
    // 比较运算符重载, 其余类似
    friend bool operator==(const MyInteger &mint1, const MyInteger &mint2);
private:
    int val;
    int * hide;
public:
    MyInteger();
    MyInteger(int);
    MyInteger(int, int);
    MyInteger(const MyInteger& mint);
    ~MyInteger();
    // 赋值运算符重载
    MyInteger &operator=(MyInteger &mint);
    // 函数调用运算符重载
    void operator()(int);

    void show() const;
};

// 声明
MyInteger operator+(const MyInteger &mint1, const MyInteger &min2);
MyInteger operator+(const MyInteger &mint, int val);
MyInteger operator+(int val, const MyInteger &mint);

std::ostream& operator<<(std::ostream &out, const MyInteger &mint);

MyInteger& operator++(MyInteger &mint);
MyInteger operator++(MyInteger &mint, int);

bool operator==(const MyInteger &mint1, const MyInteger &mint2);

#endif //OPERATOROVERLOADING_MYINTEGER_H

源文件

/**
 * OperatorLoading.cpp
 */
#include "MyInteger.h"

MyInteger::MyInteger() {
    this->val = 0;
    this->hide = nullptr;
}

MyInteger::MyInteger(int val) {
    this->val = val;
    this->hide = nullptr;
}

MyInteger::MyInteger(int val, int hide) {
    this->val = val;
    this->hide = new int(hide);
}

MyInteger::MyInteger(const MyInteger &mint) {
    this->val = mint.val;
    if (mint.hide == nullptr) {
        this->hide = nullptr;
    }
    else {
        this->hide = new int(*mint.hide);
    }
}

MyInteger::~MyInteger() {
    if (this->hide != nullptr) {
        delete this->hide;
        this->hide = nullptr;
    }
}

void MyInteger::show() const {
    std::cout << "The val is " << this->val << std::endl;
}

// 加法运算符重载,减、乘、除同理
MyInteger operator+(const MyInteger &mint1, const MyInteger &mint2) {
    MyInteger tmp;
    tmp.val = mint1.val + mint2.val;
    return tmp;
}

MyInteger operator+(const MyInteger &mint, int val) {
    MyInteger tmp;
    tmp.val = mint.val + val;
    return tmp;
}

MyInteger operator+(int val, const MyInteger &mint) {
    MyInteger tmp;
    tmp.val = val + mint.val;
    return tmp;
}

// 左移运算符重载
std::ostream& operator<<(std::ostream &out, const MyInteger &mint) {
    out << mint.val;
    return out;
}

// 自增运算符重载,自减同理
// 前置自增
MyInteger& operator++(MyInteger &mint) {
    mint.val = mint.val + 1;
    return mint;
}
// 后置自增
MyInteger operator++(MyInteger &mint, int) {
    MyInteger tmp = mint;
    mint.val = mint.val + 1;
    return tmp;
}

// 比较运算符重载
bool operator==(const MyInteger &mint1, const MyInteger &mint2) {
    bool flag = true;
    if (mint1.val != mint2.val) {
        flag = false;
    }
    return true;
}

// 函数调用运算符重载
void MyInteger::operator()(int val) {
    this->val = val;
}

// 赋值运算符重载
// 编译器默认提供赋值运算符重载, 单只支持浅拷贝, 因此需要重写
MyInteger &MyInteger::operator=(MyInteger &mint) {
    this->val = mint.val;
    if (mint.hide == nullptr) {
        this->hide = nullptr;
    }
    else {
        this->hide = new int(*mint.hide);
    }
    return *this;
}

主函数源文件

/**
 * main.cpp
 */
#include <iostream>
#include "MyInteger.h"

int main() {
    std::cout << "Hello, World!" << std::endl;
    MyInteger m1 = MyInteger(1);
    MyInteger m2 = MyInteger(2);
    MyInteger m3;
    // 加法运算符重载测试
    // m3 = m1 + m2;        // TODO: 这里出错了, 实现的赋值运算符重写冲突
    m3.show();
    MyInteger m4 = m1 + m2;
    m4.show();
    MyInteger m5 = m1 + m2 + m3;
    m5.show();
    MyInteger m6 = m1 + 1;
    m6.show();
    MyInteger m7 = 1 + m1;
    m7.show();
    // 自增运算符重载测试
    std::cout << m6 << "\t" << m7 << std::endl;
    std::cout << ++m6 << "\t" << ++(++m6) <<std::endl;
    std::cout << m7++ << "\t" << m7 << std::endl;
    // 赋值运算符重载测试
    m7 = m6 = m1;
    m7.show();
    m6.show();
    // 比较运算符重载测试
    if (m7 == m1) {
        std::cout << "m7 == m1" << std::endl;
    }
    else {
        std::cout << "m7 != m1" << std::endl;
    }
    // 函数调用运算符重载测试
    MyInteger m8;
    m8(500);
    m8.show();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值