首页 > 其他 > 详细

运算符重载

时间:2017-04-26 23:22:54      阅读:194      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#include "stdafx.h"

struct BigNumber {
private:
    unsigned int low;
    unsigned int high;
public:
    BigNumber(int x, int y)
    {
        this->low = x;
        this->high = y;
    }
    BigNumber operator ++();
    void operator =(const BigNumber &n);
    bool operator >(const BigNumber &n);
    bool operator ==(const BigNumber &n);
    void print();

};

BigNumber BigNumber::operator ++()
{
    if(this->low == 0xFFFFFFFF) {
        this->low = 0;
        this->high++;
    } else
        this->low++;
    return *this;
}
void BigNumber::operator =(const BigNumber &n)
{    
    this->low = n.low;
    this->high = n.high;
}

bool BigNumber::operator >(const BigNumber &n)
{
    if (this->high > n.high)
        return true;
    else if (this->high < n.high)
        return false;
    else if (this->low > n.low)
        return true;
    else if (this->low <= n.low)
        return false;
}

bool BigNumber::operator ==(const BigNumber &n)
{
    if (this->high == n.high && this->low == n.low)
        return true;
    else
        return false;
}

void BigNumber::print()
{
    printf("low: %u, high: %u\n", this->low, this->high);
}
    

int main(int argc, char* argv[])
{
    //BigNumber n(1, 2);
    BigNumber n(0xFFFFFFFF, 2);
    n.print();
    n++;
    n.print();

    BigNumber n1(1, 2);
    BigNumber n2 = n1;
    n2.print();
    printf("n1 %s n2\n", n1 == n2 ? "==" : "<");
    n2++;
    printf("n1 %c n2\n", n1 > n2 ? ‘>‘ : ‘<‘);

    getchar();
    return 0;
}

运算符重载

原文:http://www.cnblogs.com/roadmap99/p/6771510.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!