Hi,

I'm having some trouble trying to overload the operator ++. When I try to overload it, it does not output the correct output that I want. I want to try to make the first input to be incremented, but I cannot find a way how. Can someone please help me out?

Here's an output that I am trying to achieve. "A" is the first output that I am trying to increment, which is 34.

For example:

the input: 34 45

the output:

cout << A++ << A

34 35

cout << ++A << A

36 36

Here's my code:

HugeInteger.h

Code:#include <iostream> using std::cin; using std::cout; using std::endl; using std::istream; using std::ostream; class HugeInteger { public: HugeInteger( long long value = 0LL ); HugeInteger( const char *str ); HugeInteger & operator +=( const HugeInteger & RHS); HugeInteger & operator -=( const HugeInteger & RHS); HugeInteger operator -(void)const; HugeInteger operator ~(void)const; // will overload ~ as abs operator bool operator ==( const HugeInteger & RHS)const; bool operator <( const HugeInteger & RHS)const; bool operator !(void)const; void input( const char *str ); operator double(void)const; HugeInteger & operator ++ (void); HugeInteger operator ++ (int); private: const static int MaxDigit = 40; bool negative; short hugeInt[ MaxDigit ]; friend istream & operator >> (istream & src, HugeInteger & value); friend ostream & operator << (ostream & dest, const HugeInteger & value); }; //overloads the unary + operator for the HugeInteger class //will return true if operand is != zero bool operator +( const HugeInteger);

HugeInteger.cpp

Code:#include "HugeInteger.h" // ctor converts a long long into a HugeInteger HugeInteger::HugeInteger( long long value ) { // set all MaxDigit digits to zero to start this->negative = false; if (value < 0LL){ // 0LL is constant literal 0 of type long long this->negative = true; value = - value; // make the value positive } for( int i = 0; i < MaxDigit; i++ ) this->hugeInt[i] = 0; // convert individual digits of input value into a HugeInteger for( int j = MaxDigit-1; j >= 0 && value != 0LL; j-- ) { this->hugeInt[j] = value % 10; value /= 10; } // test to make sure that HugeInteger was able to contain value if (value != 0LL){ *this = 0LL; // set to -0, to signal overflow this->negative = true; // Possibly should increase value assigned } // to MaxDigit to fix this problem. } // converts string into a HugeInteger object HugeInteger::HugeInteger( const char *str ) { this->input( str ); } // Adds into the HugeInteger pointed to by the "this" pointer the HugeInteger op. // Then the calculated result is returned HugeInteger & HugeInteger::operator +=( const HugeInteger &op ) { // if the signs of the 2 numbers are opposites, we need to do subtraction // remember that x + y == x - (-y) if ((this->negative && !(op.negative)) || (!(this->negative) && op.negative)) return this->operator -=(-op); // NOTE: From here on, we know the two operands are the same sign int carry = 0; for (int i = MaxDigit - 1; i >= 0; i-- ) { this->hugeInt[i] = this->hugeInt[i] + op.hugeInt[i] + carry; if (this->hugeInt[i] > 9 ){ this->hugeInt[i] -= 10; carry = 1; } else carry = 0; } // test for overflow if (carry == 1){ *this = 0LL; // just set to -0 (LL is for type long long) this->negative = true; // to signal that an overflow occurred } return *this; } // Subracts from the HugeInteger pointed to by the "this" pointer the HugeInteger op // Then the calculated value is returned. HugeInteger & HugeInteger::operator -=( const HugeInteger &op ) { // if the signs of the 2 numbers are opposites, we need to do addition // remember that x - y == x + (-y) if ((this->negative && !(op.negative)) || (!(this->negative) && op.negative)){ return this->operator +=(-op); } // NOTE: From here on, we know the two operands are the same sign HugeInteger bigger, smaller; // used to make code easier to understand if ((*this) == op){ *this = 0LL; this->negative = false; return *this; } else if ( (~op) < (~(*this))){ // is magnitude of LHS > RHS bigger = *this; smaller = op; } else { // magnitude of RHS > LHS smaller = *this; bigger = op; *this = -(*this); // result needs to be negated } // subtract smaller (in magnitude) from biggger (in magnitude) int borrow = 0; short top, bottom, result; for ( int i = MaxDigit-1; i >= 0; i-- ) { top = bigger.hugeInt[i]; bottom = smaller.hugeInt[i]; if (borrow == 1) { top -= 1; borrow = 0; } result = top-bottom; if ( result < 0) //if true, we need to borrow { this->hugeInt[i] = (10+top)-bottom; // or result + 10; borrow = 1; } else this->hugeInt[i] = result; } return *this; } // Computes the negation of a HugeInteger HugeInteger HugeInteger::operator -(void)const { HugeInteger temp = *this; temp.negative = !temp.negative; return temp; } // Computes and returns absolute value of operand HugeInteger HugeInteger::operator ~(void)const { HugeInteger temp = *this; temp.negative = false; return temp; } bool HugeInteger::operator ==( const HugeInteger &op )const { if(this->negative != op.negative) return false; for(int i = MaxDigit-1; i >= 0; i--) if(this->hugeInt[i] != op.hugeInt[i]) return false; return true; } bool HugeInteger::operator < ( const HugeInteger &op )const { if(this->negative && (op.negative == false)) return true; if(!(this->negative) && op.negative) return false; for(int i = MaxDigit-1; i >= 0; i--) { //if the numbers are both negative if(this->hugeInt[i] < op.hugeInt[i]) { if(!(this->negative)) return true; else //if both numbers are negative return false; } //if the numbers are both positive else if(this->hugeInt[i] > op.hugeInt[i]) { if(!(this->negative)) return false; else //if both numbers are negative return true; } } return false; } // Is_Zero operator bool HugeInteger::operator !()const { for(int i = MaxDigit-1; i >= 0; i--) if(this->hugeInt[i] == 0) return true; return false; } void HugeInteger::input( const char *str ) { // assume positive for now this->negative = false; // init. to all zeros first for( int i = 0; i < MaxDigit; i++ ) { this->hugeInt[i] = 0; } int len = (int)strlen( str ); int k = 0; // if sign part of string, we need to process // if + sign, we ignore since we start with assumption that // input string represents a positive number if ((str[k] == '-') || (str[k] == '+')){ if (str[k] == '-'){ // if negative, set negative member to true this->negative = true; } ++k; // go to next char in string "str" --len; // length of number is one less } if (len > MaxDigit) { // if true, too many digits this->negative = true; // return -0 to signal there return; // was a problem } for( int j = MaxDigit - len; j < MaxDigit; j++, k++ ) { if (isdigit(str[k])){ this->hugeInt[j] = str[k] - '0'; } else // a problem with the string input { *this = 0LL; // just set to -0 (LL is for type long long) this->negative = true; // to signal there was a problem break; } } } // Pre-increment operator HugeInteger & HugeInteger::operator ++ () { for(int i = MaxDigit-1; i >= 0; i--) { this->hugeInt[i] += this->hugeInt[i]; } return *this; } // Post-increment operator HugeInteger HugeInteger::operator ++ (int) { HugeInteger temp = *this; for(int i = MaxDigit-1; i >= 0; i--) { temp.hugeInt[i] + temp.hugeInt[i]; } return temp; } bool operator +( const HugeInteger value) // is not zero { return !(!value); } // overloads the >> operator as a friend function istream & operator >> (istream & src, HugeInteger & value) { char input_string[HugeInteger::MaxDigit+2]; src >> input_string; value.input(input_string); return src; } // overloads the << operator as a friend function ostream & operator << (ostream & dest, const HugeInteger & value) { // find first non-zero digit int i; for( i = 0; (value.hugeInt[i] == 0) && (i < HugeInteger::MaxDigit); i++ ) { ; } // if all zeros, just output a single 0 if (i == HugeInteger::MaxDigit) { dest << "0"; return dest; } // check if we need to ouput a negative sign if (value.negative) dest << '-'; // output remaining digits for( ; i < HugeInteger::MaxDigit; i++) dest << (short) value.hugeInt[i]; return dest; }Main.cpp

Code:#include "HugeInteger.h" #include <iso646.h> int main() { HugeInteger A,B,C,D; // input value for A & B cout << "****** Test << & >> operators ******\n\n"; cout << "Input values for A and B: "; cin >> A >> B; cout << "\nA = " << A << "\nB = " << B; // test increment operators cout << "\n****** Test increment and decrement operators ******\n"; cout << "\nA = " << A << "\nB = " << B << "\n"; cout << "\ncout << A++ << A\n"; cout << A++ << " " << A; cout << "\n\ncout << ++A << A\n"; cout << ++A << " " << A << "\n"; system("pause"); return 0; }