运算符‘++’‘--’分别重载为非成员函数,实现时钟类自增自减运算

自增运算符++与自减运算符--运算逻辑大同小异,这里主要分析++运算符,--运算符后面有具体代码实现!

一 分析

1.需要实现单目运算符++的重载则要分别定义两个函数实现重载!

前置++与后置++函数的实现,前置++会改变表达式的值,例如++a,表达式++a的值会+1,

但是后置++,例如a++,只会使a的值+1,表达式a++的值还是原来a的值。

2.需要实现时钟类的自增运算需要注意运算逻辑,具体在第二部分有详细说明。

3.重载为非成员函数需要,将原函数定义为友元函数以获得时钟类私有成员的访问权限!

二  注意事项

1.运算符++为单目运算符,重载为非成员函数格式如下。

前置++与后置++格式分别如下:

 Clock operator ++(Clock &p);
   Clock operator ++(Clock &p, int);//需要记住,运算符++后置时需要两个形参,int类型做后缀,具体原因找度娘。

2.注意前置++与后置++的运算逻辑

前置++逻辑实现

Clock operator ++(Clock &p) {
    p.s++;
    //时间自增运算逻辑。
    if (p.s == 60)
    {
        p.s = 0;
        p.m++;
        if (p.m == 60)
        {
            p.m = 0;
            p.h++;
            if (p.h == 24)
            {
                p.h = 0;
            }
        }
    }
    return p;
}

后置++逻辑实现

Clock operator ++(Clock &p, int) {//需要记住,运算符++后置时需要两个形参,int类型做后缀,具体原因找度娘。
    Clock old = p;
    ++p;
    return old;
}

三 代码实现

例:

#include<iostream>
using namespace std;

class Clock {
private:
    int h, m, s;
public:
    Clock(int nh = 0, int nm = 0, int ns = 0)
    {
        h = nh;
        m = nm;
        s = ns;
    }
    void show()
    {
        cout << h << ":" << m << ":" << s << endl;
    }
    friend Clock operator ++(Clock &p);//需要友元函数,获得访问时钟类私有成员权限。
    friend  Clock operator ++(Clock &p, int);
};

Clock operator ++(Clock &p) {
    p.s++;
    //时间自增运算逻辑。
    if (p.s == 60)
    {
        p.s = 0;
        p.m++;
        if (p.m == 60)
        {
            p.m = 0;
            p.h++;
            if (p.h == 24)
            {
                p.h = 0;
            }
        }
    }
    return p;
}
//后置++函数表达式值并没有改变,例如a++只改变了a的值,a++表达式值,没有变!!!!!!!!!!!!

Clock operator ++(Clock &p, int) {//需要记住,运算符++后置时需要两个形参,int类型做后缀,具体原因找度娘。
    Clock old = p;
    ++p;
    return old;
}




int main() {

    Clock first(23, 59, 59);

    first.show();

    (first++).show();

    (++first).show();

    return 0;

}
#include<iostream>
using namespace std;

class Clock {
private:
    int h, m, s;
public:
    Clock(int nh = 0, int nm = 0, int ns = 0)
    {
        h = nh;
        m = nm;
        s = ns;
    }
    void show()
    {
        cout << h << ":" << m << ":" << s << endl;
    }
    friend Clock operator --(Clock &p);//需要友元函数,获得访问时钟类私有成员权限。
    friend  Clock operator --(Clock &p, int);
};

Clock operator --(Clock &p) {
    p.s--;
    //时间自减运算逻辑。
    if (p.s == -1)
    {
        p.s = 59;
        p.m--;
        if (p.m == -1)
        {
            p.m = 59;
            p.h--;
            if (p.h == -1)
            {
                p.h = 23;
            }
        }
    }
    return p;
}
//后置--函数表达式值并没有改变,例如a--只改变了a的值,a--表达式值,没有变!!!!!!!!!!!!

Clock operator --(Clock &p, int) {//需要记住,运算符--后置时需要两个形参,int类型做后缀,具体原因找度娘。
    Clock old = p;
    --p;
    return old;
}




int main() {

    Clock first(0, 0, 0);

    first.show();

    (first--).show();

    (--first).show();

    return 0;

}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是重载运算符++和--的Complex实现代码: ```c++ #include <iostream> using namespace std; class Complex { private: double real, imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex& operator++(); // 前置++ Complex operator++(int); // 后置++ Complex& operator--(); // 前置-- Complex operator--(int); // 后置-- void show() const { cout << "(" << real << ", " << imag << ")" << endl; } }; Complex& Complex::operator++() { real++; return *this; } Complex Complex::operator++(int) { Complex temp(*this); real++; return temp; } Complex& Complex::operator--() { real--; return *this; } Complex Complex::operator--(int) { Complex temp(*this); real--; return temp; } int main() { Complex a(1, 2); (++a).show(); // 输出:(2, 2) a.show(); // 输出:(2, 2) (a++).show(); // 输出:(2, 2) a.show(); // 输出:(3, 2) (--a).show(); // 输出:(2, 2) a.show(); // 输出:(2, 2) (a--).show(); // 输出:(2, 2) a.show(); // 输出:(1, 2) (a--).show(); // 输出:(1, 2) a.show(); // 输出:(0, 2) return 0; } ``` 在上面的代码中,我们为Complex重载运算符++和--,包括前置++、后置++、前置--和后置--。对于前置++和前置--,我们直接修改实部real的值,并返回*this的引用;对于后置++和后置--,我们先用*this的值初始化一个临时对象temp,然后再修改实部real的值,并返回这个临时对象temp,从而实现后置++和后置--。在主程序中,我们对Complex对象a进行了多次++和--运算,并输出了运算结果,验证了运算符的正确性。注意,由于后置++和后置--返回的是临时对象,因此如果直接调用a--.show(),则不能正确输出a--的结果,必须使用(a--).show()的形式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值