title: Big Number Implementation in c++
body: <p>I am trying to implement Big Number in c++ with strings.
  First I am doing operator overloading for '+' operator, addition for digit length > 3 is coming correct but for less than 3 is giving garbage value after result. I am not understanding why this is happening.
  Here is my code.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  #define SIZE 100
  using namespace std;
  class BigNumber
  {
      char *chNumber;
      long nLength,nMaxLength;
      public :
          BigNumber()
          {
              nLength = nMaxLength = 0;
              Init();
          }
          BigNumber(long length)
          {
              nLength = length;
              nMaxLength = length;
              Init();
          }
          BigNumber(const char *str,long length)
          {
              nLength = strlen(str);
              nMaxLength = length;            
              Init();
              memset(chNumber,0,sizeof(char));
              strncpy(chNumber,str,length);
          }
          /*** copy constructor **/
          BigNumber(const BigNumber &amp;source)
          {
              nLength = strlen(source.chNumber);
              nMaxLength = source.nMaxLength;
              if(source.chNumber)
              {
                  Init();
                  strncpy(chNumber,source.chNumber,nLength);
              }
              else
                  chNumber = 0;
          }
          void Init()
          {
              chNumber = new char[nMaxLength + 5];
              memset(chNumber,0,sizeof(char));
          }
          ~BigNumber()
          {
              delete[] chNumber;
              chNumber = 0;
              nLength = 0;
          }
          char* getNumber()
          {
              return chNumber;
          }
          const long size() const
          {
              return nLength;
          }
          const long capacity() const
          {
              return nMaxLength;
          }
          friend long maxsize(BigNumber &amp;obj1,BigNumber &amp;obj2) 
          {
              return (obj1.size() &gt; obj2.size()) ? obj1.size():obj2.size();
          }
          friend ostream&amp; operator&lt;&lt;(ostream &amp;out,BigNumber &amp;obj)
          {
              //out&lt;&lt;"String is "&lt;&lt;obj.chNumber&lt;&lt;" of length "&lt;&lt;obj.nLength
              //  &lt;&lt;" and max length allocated is "&lt;&lt;obj.nMaxLength;
              out&lt;&lt;obj.chNumber;
              return out;
          }
  
          friend istream&amp; operator&gt;&gt;(istream &amp;in,BigNumber &amp;obj)
          {
              obj.Init();
              in&gt;&gt;obj.chNumber;
              obj.nLength = strlen(obj.chNumber);
              return in;
          }   
          BigNumber &amp;operator=(const BigNumber &amp;obj)
          {
              if(this == &amp;obj)
              {
                  return *this;
              }
              delete[] chNumber;
              chNumber = 0;
              nLength = strlen(obj.chNumber);
              Init();
              strncpy(chNumber,obj.chNumber,nLength);
              return *this;
          }
          void operator=(char * str)
          {
              nLength = strlen(str);
              Init();
              strncpy(chNumber,str,nLength);
          }
          friend BigNumber reverse(const BigNumber &amp;obj1)
          {
              long length = obj1.size();
              int m=0;
              BigNumber obj2(length+5);
              for(int i=length-1;i&gt;=0;--i)
              {
                  obj2.chNumber[m++]=obj1.chNumber[i];
              }
              obj2.chNumber[m]='\0';
              //cout&lt;&lt;obj2.chNumber&lt;&lt;endl;
              obj2.nLength = m;
              return obj2;
          }
          friend BigNumber operator+(BigNumber &amp;obj1,BigNumber &amp;obj2)
          {
              long newLength = maxsize(obj1,obj2);
              BigNumber obj3(newLength + 5);
              int length1 = obj1.size();
              int length2 = obj2.size();
              int i,j,carry=0,num,m=0;
              for(i=length1-1,j=length2-1; i&gt;=0 || j&gt;=0 ; --i,--j)
              {
                  if(i&gt;=0 &amp;&amp; j&gt;=0)
                  {
                      num = (obj1.chNumber[i]-'0') + (obj2.chNumber[j]-'0') + carry;
                  }
                  else if(i&gt;=0)
                  {
                      num = obj1.chNumber[i] - '0' + carry;
                  }
                  else if(j&gt;=0)
                  {
                      num = obj2.chNumber[j] - '0' + carry;
                  }
                  carry = num/10;
                  num = num%10;
                  obj3.chNumber[m++] = num+'0';
              }
              obj3.chNumber[m]='\0';
              obj3.nLength = m; 
              BigNumber obj4 = reverse(obj3);
              cout&lt;&lt;obj4&lt;&lt;endl;
              return reverse(obj3);
          }
  };
  
  void test_addition()
  {
      BigNumber n1("42",3),n2("1",2),n3;
      n3 = n1 + n2;
      cout&lt;&lt;n3&lt;&lt;endl;
      n1 = "123";
      n2 = "345";
      n3 = n1 + n2;
      cout&lt;&lt;n3&lt;&lt;endl;
  }
  int main()
  {
      test_addition();
      return 0;
  }
  </code></pre>
  
  <p>Before returning from operator+() function, I am printing sum its giving correct output and after returning I am printing is giving junk values after result.</p>
  
  <p>Thank You</p>