I was trying to implement Big Integer Implementation. I wanted to start with the basic operation of addition. I am having some problems with operator overloading part, can someone help me out?
You've overloaded operator+=, but you're trying to use operator+.
Fortunately, operator+ can be implemented in terms of operator+=.
Unfortunately, operator+= is not implemented correctly. It should modify the object it is invoked on, and it should return a reference to that same object.
You also haven't overloaded the insertion operator, so line 84 won't work.
[Edit: If I were you, I'd start with the insertion operator so you can 'cout' a BigInt, then just create one such as you have on line 80 and 81 and make sure that's working correctly before I worked on anything else.]
Thanks a lot for your replies cire and duoas. I took your suggestions and looked at other code piece and came up with a working program :)
I have to optimize it though, here is the modified working program.
operator +
creates a new BigInt and returns it, without modifying *this
If you make a copy constructor like I asked you to, you can define + in terms of +=
1 2 3 4 5 6 7 8 9 10
BigInt& operator +=(const BigInt& a)
{
//add a to *this here.
return *this;
}
BigInt operator +(const BigInt& a) const
{
return BigInt(*this) += a;
}
You also need to be careful now about the size of your operands. Before they were always 200 digits, so you couldn't have any troubles. Now, what if you add a really big number to a smaller one:
1 2 3 4 5
BigInt B1("12");
BigInt B2("987654321");
BigInt B3;
B3 = B1 + B2; // B1 isn't big enough to handle adding all B2's digits
By the way, you should make a policy now on how you will handle and store zero. Right now zero is {0}, where (digits.size() == 1). Otherwise your number should take care to not have any extraneous leading zeros in the digits. (Once you get into doing multiplications the number of leading zeros can grow quickly.)
Hi Cerie & Duoas,
I took your suggestions and modified my program quite a bit. I was having some difficult understanding copy constructors and assignment operators. I think I kind of understood the need for it, but still not convinced on when & how to use them. I think this will improve with more coding experience. I am attaching my latest modified piece of code, I still have some trivial questions which will follow the code. Please help me out, it will improve my understanding.
Questions:
1) How can I optimize my code in line 11. The constructor is currently passing number of zeros equal to output size
2) I have used default copy constructor(the one used by compiler), is user defined type usually different?
I'm not at my PC ATM so I can't say much more than what a quick glance will do, but you are looking much better.
Line 11 shouldn't need more than one zero -- such inputs should not be permitted to modify the internal representation of your bignum. (Remember what I said about a policy on leading zeros?)
Line 12 should have the keyword "const" just before the semicolon.
I notice that you are messing with a 'temp' bignum again in your += operator function. You really don't need it. All you have to do is make sure that this->digits.size() is not less than a.digits.size(). Then add with carry as long as there are digits in a. Then carry into the remaining digits of *this.
Re copy ctor: yes, it will be different if necessary. In this case, the default does everything correctly.
Looking much better. There is no reason to explicitly define operator= (the defaulted version will be correct,) but if you choose to do so, the function should return a reference to BigInt in order to correctly simulate the semantics of built-in arithmetic types.
Since your operator= takes a copy of a BigInt, the following would be an efficient definition (assuming you don't just let the compiler do the work.)
Thanks a lot guys. I was able to modify it further and include the above suggestions. The code looks more stable and neat now. Here is the updated code.