I'm trying to implement operator overloading. I think I was successful with overloading the input and output function. Do I need to keep the current functions and add functions such as

bool operator == (const HugeInteger&, const HugeInteger&);

My program is listed below. Please point me in the right direction. Thanks


Header File

#include <iostream>

using namespace std;

class HugeInteger {
  friend bool isEqualTo (const HugeInteger&, const HugeInteger&);
  friend bool isNotEqualTo (const HugeInteger&, const HugeInteger&);
  friend bool isLessThan (const HugeInteger&, const HugeInteger&);
  friend bool isLessThanOrEqualTo (const HugeInteger&, const HugeInteger&);
  friend bool isGreaterThan (const HugeInteger&, const HugeInteger&);
  friend bool isGreaterThanOrEqualTo (const HugeInteger&, const HugeInteger&);
  friend bool isZero (const HugeInteger&);

public:
  HugeInteger(int = 0);
     
  void input(istream&); 
  void output(ostream&) const;
     
  HugeInteger& add(const HugeInteger&);
  HugeInteger& subtract(const HugeInteger&);
  HugeInteger& multiply(const HugeInteger&);    

 private:
  void multiply(const int);
  void pow10(int); 

  int arraySize[40];
  int size;
  int sign;
};

istream& operator >> (istream&, HugeInteger&);
ostream& operator << (ostream&, const HugeInteger&);

Main Program

#include "stdafx.h"
#include "HugeInteger2.h"
#include<iostream>
using namespace std;

// default constructor; conversion constructor that converts
// a long integer into a HugeInteger object
HugeInteger::HugeInteger( int value )
  : size(0)
{
  // check value sign
  if (value < 0)
  {
    sign = -1;
    value = abs(value);
  }
  else
    sign = 1;

  // parse value into the corresponding digits
  do {
    arraySize[size++] = value % 10;
    value /= 10;
  }
  while (value);
} 

//read in value from the user to set huge integer
void HugeInteger::input(istream& inputSet)
{   
  char BigInteger[40];
    
  
  inputSet.getline(BigInteger, strlen(BigInteger)-1);

  // construct huge integer
  size = strlen(BigInteger);
  int index = 0;
  if (BigInteger[0] == '-')
  {
    sign = -1;
    --size;
  }
  else
    sign = 1;

  const int StringSize = strlen(BigInteger);
  // copy digits from the buffer
  for (; index < size; ++index)
    arraySize[index] = BigInteger[StringSize - index - 1] - '0';
}

void HugeInteger::output(ostream& outputSet) const
{
  // check for positive values and a zero to print a unary minus
  if (sign == -1 && !isZero(*this))
    cout << "-";
  // display all number's digits
  for (int index = size - 1; index >= 0; --index)
    outputSet << arraySize[index];
}

HugeInteger& HugeInteger::add(const HugeInteger& number2)
{
  // if second operand is negative subtract it's module from the first one
  if (sign == 1 && number2.sign == -1)
  {
    HugeInteger temp = number2;
    temp.sign = 1;
    subtract(temp);
    
    return *this;
  }
   
  // if the first one is negative, subtract it's module from the second operand
  if (sign == -1 && number2.sign == 1)
  {
    HugeInteger temp = *this;
    temp.sign = 1;
    *this = number2;
    subtract(temp);

    return *this;
  }  

  //accept adding: positive + positive or negative + negative
  int carry = 0;
  int index;
  for (index = 0; index < size || index < number2.size; ++index)
  {
    // get digits
    int alpha = 0, beta = 0;
    if (index < size)
      alpha = arraySize[index];
    if (index < number2.size)
      beta = number2.arraySize[index];

    // calculate new carry
    int newCarry = alpha + beta + carry;
    // set new digit value
    arraySize[index] = newCarry % 10;
    carry = newCarry / 10;
  }

  // check size increment for nonzero carry
  if (index >= size)
    size = index;
  if (carry)
    arraySize[size++] = carry;

  // delete trailing zeros
  while (size > 1 && arraySize[size - 1] == 0)
    --size;

  return *this;
}

HugeInteger& HugeInteger::subtract(const HugeInteger& number2)
{
  // if first < second and both are negative, subtract from the second operand first 
  if (isLessThan(*this, number2) &&
      !(sign == -1 && number2.sign == -1))
  {
    HugeInteger temp = *this;
    *this = number2;
    subtract(temp);
    sign =1;

    return *this;
  }

  // if second is negative, add to the first 
  if (sign == 1 && number2.sign == -1)
  {
    HugeInteger temp = number2;
    temp.sign = 1;
    add(temp);
    
    return *this;
  }
   
  // if first is negative add two negative numbers
  if (sign == -1 && number2.sign == 1)
  {
    HugeInteger temp = number2;
    temp.sign = -1;
    add(temp);

    return *this;
  } 

  // accept only: + + and - -
  int carry = 0;
  int tempSize = size;
  for (int index = 0; index < tempSize || index < number2.size; ++index)
  {
    int alpha = 0, beta = 0;
    if (index < tempSize)
      alpha = arraySize[index];
    if (index < number2.size)
      beta = number2.arraySize[index];

    int newCarry = alpha - beta - carry;
    
    if (newCarry < 0)
    {
      arraySize[index] = 10 + newCarry;
      carry = 1;
    }
    else {
      arraySize[index] = newCarry;
      carry = 0;
    }

    if (arraySize[index])
      size = index + 1;
  }

  while (size > 1 && arraySize[size - 1] == 0)
    --size;

  return *this; 
}

void HugeInteger::pow10(int power)
{
  // increment size and add zero to the number end
  while (power--)
  {
    for (int index = size; index > 0; --index)
      arraySize[index] = arraySize[index - 1];
    arraySize[0] = 0;
    ++size;
  }
}

void HugeInteger::multiply(const int number)
{
  int carry = 0;
  
  for (int index = 0; index < size || carry; ++index)
  {
    int alpha = 0;
    if (index < size)
      alpha = arraySize[index];
    int newCarry = alpha * number + carry;
    arraySize[index] = newCarry % 10;
    carry = newCarry / 10;

    if (index == size)
    {
      ++size;
      break; 
    }
  }
}

HugeInteger& HugeInteger::multiply(const HugeInteger& number2)
{
  HugeInteger result(0l);

  // memorize result sign
  int setSign = sign * number2.sign;
  this->sign = 1;
  // multiply number by each digit of the second number and add with offset 10^x
  for (int index = 0; index < number2.size; ++index)
  {
    HugeInteger temp(*this);
    temp.multiply(number2.arraySize[index]);
    temp.pow10(index);
    result.add(temp);
  }

  result.sign = setSign;

  return *this = result;
}

istream& operator >> (istream& inputSet, HugeInteger& BigNumber)
{
  BigNumber.input(inputSet);
  return inputSet;
}

ostream& operator << (ostream& outputSet, const HugeInteger& BigNumber)
{
  BigNumber.output(outputSet);
  return outputSet;
}

bool isEqualTo (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  if (LeftSide.size != RightSide.size ||
      LeftSide.sign != RightSide.sign)
    return false;
  for (int index = LeftSide.size - 1; index >= 0; --index)
    if (LeftSide.arraySize[index] != RightSide.arraySize[index])
        return false;
  return true;  
}

bool isNotEqualTo (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  return !(isEqualTo(LeftSide, RightSide));
}


bool isGreaterThan (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  return isLessThan(RightSide, LeftSide);
}

bool isLessThan (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  if (LeftSide.sign != RightSide.sign)
    return LeftSide.sign < RightSide.sign;
  
  if (LeftSide.sign == 1)
  {
    if (LeftSide.size != RightSide.size)
      return LeftSide.size < RightSide.size;
   
    for (int index = LeftSide.size - 1; index >= 0; --index)
      if (LeftSide.arraySize[index] != RightSide.arraySize[index])
        return (LeftSide.arraySize[index] < RightSide.arraySize[index]);

    return false;
  }
  else
  {
    HugeInteger LeftSide1 = LeftSide;
    HugeInteger RightSide1 = RightSide;
    LeftSide1.sign = 1;
    RightSide1.sign = 1;

    return isGreaterThan(LeftSide1, RightSide1);
  }
}

bool isGreaterThanOrEqualTo (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  return !isLessThan(LeftSide, RightSide);
}

bool isLessThanOrEqualTo (const HugeInteger& LeftSide, const HugeInteger& RightSide)
{
  return !isLessThan(RightSide, LeftSide);
}
 
bool isZero (const HugeInteger & LeftSide)
{
  if (LeftSide.size == 1 && LeftSide.arraySize[0] == 0)
    return true;
  else 
  return false;
}


int main()
{
   HugeInteger number1;
   HugeInteger number2;
   HugeInteger temp;

   cout << "Enter the first number.\n\n";
   cout << "number1 = ";
   cin >> number1;
   cout << "\nEnter the second number.\n\n";
   cout << "number2 = ";
   cin >> number2;
   temp = number1;
   cout << "\n";
   cout << "number1 + number2 = " << number1.add(number2) << endl;
   number1 = temp;
   cout << "\n";
   cout << "number1 - number2 = " << number1.subtract(number2) << endl;
   number1 = temp;
   cout << "\n";
   cout << "number1 * number2 = " << number1.multiply(number2) << endl;
   number1 = temp;
   cout << boolalpha;
   cout << "\n";
   cout << "number1 < number2 = " << (isLessThan(number1, number2)) << endl;
   cout << "\n";
   cout << "number1 <= number2 = " << (isLessThanOrEqualTo(number1, number2)) << endl;
   cout << "\n";
   cout << "number1 > number2 = " << (isGreaterThan(number1, number2)) << endl;
   cout << "\n";
   cout << "number1 >= number2 = " << (isGreaterThanOrEqualTo(number1, number2)) << endl;
   cout << "\n";
   cout << "number1 == number2 = " << (isEqualTo(number1, number2)) << endl;
   
   return 0;
}

>>Do I need to keep the current functions and add functions such as

Only if you want to or need to.

But, since you declare and define a non-default constructor you should write a copy constructor, and it would be a good idea to write a default constructor as well. Since your class doesn't use pointers you don't have to, but it's a wise idea to, write an assignment operator and a destructor for your class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.