首页 > 代码库 > 运算符重载
运算符重载
#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;
}
运算符重载