c++中的算术运算符重载

本文介绍了C++中如何通过友元和成员函数重载算术运算符,包括加、减、乘、除和取余操作。通过示例代码展示了如何定义一个名为count1的类,并重载这些运算符。在测试部分,演示了各种运算符的使用,从而理解运算符重载的工作原理和实际效果。
摘要由CSDN通过智能技术生成

算数运算符重载

c++中可以重载的算术运算符有:+-*/%

其他的不多说上代码:

#include<iostream>

using namespace std;

//双目算术运算符重载

class count1 {
//友元函数重载
    friend count1 operator+(const count1& p1, const count1& p);

    friend count1 operator-(const count1& p1, const count1& p);

    friend count1 operator*(const count1& p1, const count1& p);

    friend count1 operator%(const count1& p1, const count1& p);

    friend count1 operator/(const count1& p1, const count1& p);

private:

    int a;

    int b;

public:

    //构造函数

    count1(int a, int b) :a(a), b(b) {};

    //拷贝构造函数

    count1(const count1& p) {

         cout << "拷贝函数" << endl;

         this->a = p.a;

         this->b = p.b;

    }

    ~count1() {}

    double aValue() {

         return this->a;

    }

    double bValue() {

         return this->b;

    }

    //成员函数运算符重载

    //加号重载

    count1 operator+(const count1& p) {

         count1 temp(0, 0);

         temp.a = this->a + p.a;

         temp.b = this->b + p.b;

         return temp;

    }

    count1 operator-(const count1& p) {

         count1 temp(0, 0);

         temp.a = this->a - p.a;

         temp.b = this->b - p.b;

         return temp;

    }

    count1 operator*(const count1& p) {

         count1 temp(0, 0);

         temp.a = this->a * p.a;

         temp.b = this->b * p.b;

         return temp;

    }

    count1 operator/(const count1& p) {

         count1 temp(0, 0);

         temp.a = this->a / p.a;

         temp.b = this->b /p.b;

         return temp;

    }

    count1 operator%(count1& p) {

         count1 temp(0, 0);

         if (p.a == 0 || p.b == 0) {

             cout << "除数不能等于0";

             return temp;

         }

         temp.a = this->a %p.a;

         temp.b = this->b % p.b;

         return temp;

    }

};

//友元函数重载

count1 operator+(const count1& p1, const count1& p) {

    count1 temp(0, 0);

    temp.a = p1.a + p.a;

    temp.b = p1.b + p.b;

    return temp;

}

count1 operator-(const count1& p1, const count1& p) {

    count1 temp(0, 0);

    temp.a = p1.a - p.a;

    temp.b = p1.b - p.b;

    return temp;

}

count1 operator*(const count1& p1, const count1& p) {

    count1 temp(0, 0);

    temp.a = p1.a - p.a;

    temp.b = p1.b - p.b;

    return temp;

}

count1 operator/(const count1& p1, const count1& p) {

    count1 temp(0, 0);

    temp.a = p1.a - p.a;

    temp.b = p1.b - p.b;

    return temp;

}

count1 operator%(const count1& p1, const count1& p) {

    count1 temp(0, 0);

    temp.a = p1.a - p.a;

    temp.b = p1.b - p.b;

    return temp;

}

void test() {

    count1 add(10, 20);

    count1 add1(10, 20);

    //加号运算符

    cout << "加号运算符" << endl;

    count1 add2 = add + add1;

    cout << "add2.a=" << add2.aValue() << endl;//add2.a=20

    cout << "add2.b=" << add2.bValue() << endl;//add2.b = 40

    //减号运算符

    cout << "减号运算符" << endl;

    count1 add3 = add2 - add1;

    cout << "add3.a=" << add3.aValue() << endl;//add3.a=10

    cout << "add3.b=" << add3.bValue() << endl;//add3.b=20

    //乘号运算符重载

    cout << "乘号运算符" << endl;

    count1 add4 = add3 * add2;

    cout << "add4.a=" << add4.aValue() << endl;//add4.a=200

    cout << "add4.b=" << add4.bValue() << endl;//add4.b=800

    //除号运算符重载

    cout << "除号运算符" << endl;

    count1 add5 = add3 / add2;

    cout << "add5.a=" << add5.aValue() << endl;//add5.a=0 由于a为int类型,所以为0。若为浮点类型,则为0.5

    cout << "add5.b=" << add5.bValue() << endl;//add5.b=0 由于b为int类型,所以为0。若为浮点类型,则为0.5



   

//取余运算符重载

    cout << "乘号运算符" << endl;

    count1 add6 = add3 % add2;

    cout << "add6.a=" << add6.aValue() << endl;//10

    cout << "add6.b=" << add6.bValue() << endl;//20

}

void main1() {

    test();

    system("pause");

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值