C++运算符重载小实例

c++常见运算符重载程序样例

运算符重载为成员函数:

先来看看《c++语言程序设计》的定义:
在这里插入图片描述
样例代码:

#include <iostream>
using namespace std;
class calculate {
public:
    calculate() = default;
    calculate(int a1, int b1) : a(a1), b(b1) { }
    calculate(const calculate &item) { a = item.a; b = item.b;}
    ~calculate() { }
    //重载+号
    calculate operator+(calculate &item) {
        return calculate(a + item.a, b + item.b);
    }
    //重载减号
    calculate operator-(calculate &item) {
        return calculate(a - item.a, b - item.b);
    }
    //重载负号
    calculate operator-() {
        return calculate(-a, -b);
    }
    //重载后置++
    calculate operator++(int) {
        return calculate(a++, b++);
    }
    //重载后置--
    calculate operator--(int) {
        return calculate(a--, b--);
    }
    //重载前置++
    calculate operator++() {
        return calculate(++a, ++b);
    }
    //重载前置--
    calculate operator--() {
        return calculate(--a, --b);
    }
    void display() {
        cout << a << ' ' << b << endl;
    }
private:
    int a = 1, b = 1;
};
int main() {
    calculate x1;
    calculate x2(3, 3);
    cout << "重载+号:" << endl;
    calculate x3 = x1 + x2;
    x3.display();
    cout << "重载-号:" << endl;
    calculate x4 = x1 - x2;
    x4.display();
    cout << "重载负号:" << endl;
    calculate x5;
    x5 = -x5;
    x5.display();
    cout << "重载后置++:" << endl;
    calculate x6;
    x6++;
    x6.display();
    cout << "重载后置--:" << endl;
    calculate x7;
    x7--;
    x7.display();
    cout << "重载前置++:" << endl;
    calculate x8;
    ++x8;
    x8.display();
    cout << "重载前置--:" << endl;
    calculate x9;
    --x9;
    x9.display();
    return 0;
}

运行结果:

重载+号:
4 4
重载-号:
-2 -2
重载负号:
-1 -1
重载后置++2 2
重载后置--0 0
重载前置++2 2
重载前置--0 0

运算符重载为成员函数:

看看《c++语言程序设计》的定义:
在这里插入图片描述
样例代码:

#include <iostream>
using namespace std;
class calculate {
    friend ostream &operator<<(ostream &, calculate &); //将重载<<的函数声明为友元
    friend istream &operator>>(istream &, calculate &);  //将重载>>的函数声明为友元
    friend calculate operator+(calculate &, calculate &);
    friend calculate operator-(calculate &, calculate&);
    friend calculate operator-(calculate &);
    friend calculate operator++(calculate &, int);
    friend calculate operator--(calculate &, int);
    friend calculate operator++(calculate &);
    friend calculate operator--(calculate &);
public:
    calculate() = default;
    calculate(int a1, int b1) : a(a1), b(b1) { }
    calculate(const calculate &item) {a = item.a; b = item.b;}
private:
    int a = 1, b = 1;
};
//重载 <<
ostream &operator<<(ostream &os, calculate &item) {
    os << item.a << ' ' << item.b << endl;
    return os;
}
//重载 >>
istream &operator>>(istream &is, calculate &item) {
    is >> item.a >> item.b;
    return is;
}
calculate operator+(calculate &item1, calculate &item2) {
    return calculate(item1.a + item2.a, item1.b + item2.b);
}
calculate operator-(calculate &item1, calculate &item2) {
    return calculate(item1.a - item2.a, item1.b - item2.b);
}
calculate operator-(calculate &item) {
    return calculate(-item.a, -item.b);
}
calculate operator++(calculate &item, int) {
    return calculate(item.a++, item.b++);
}
calculate operator--(calculate &item, int) {
    return calculate(item.a--, item.b--);
}
calculate operator++(calculate &item) {
    return calculate(++item.a, ++item.b);
}
calculate operator--(calculate &item) {
    return calculate(--item.a, --item.b);
}
int main() {
    calculate x1;
    calculate x2(3, 3);
    cout << "重载+号:" << endl;
    calculate x3 = x1 + x2;
    cout << x3;
    cout << "重载-号:" << endl;
    calculate x4 = x1 - x2;
    cout << x4;
    cout << "重载负号:" << endl;
    calculate x5;
    x5 = -x5;
    cout << x5;
    cout << "重载后置++:" << endl;
    calculate x6;
    x6++;
    cout << x6;
    cout << "重载后置--:" << endl;
    calculate x7;
    x7--;
    cout << x7;
    cout << "重载前置++:" << endl;
    calculate x8;
    ++x8;
    cout << x8;
    cout << "重载前置--:" << endl;
    calculate x9;
    --x9;
    cout << x9;
    cout << "重载>>和<<:" << endl;
    calculate x10;
    cin >> x10;
    cout << x10;
    return 0;
}

运行结果:

重载+号:
4 4
重载-号:
-2 -2
重载负号:
-1 -1
重载后置++2 2
重载后置--0 0
重载前置++2 2
重载前置--0 0
重载>><<10 10
10 10

需要注意的一些地方:

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值