Multiplication and Division Operators
Learn to write the code for multiplication and division using operators.
Now, we’ll build on the previous lesson and implement the most important and nontrivial functions, i.e., operator*
, operator/
, operator%
, and their assignment versions *=
, /=
, and %=
.
Your implementation
In this lesson, we’ll guide you through the implementation of three essential operators. By building upon the concepts and implementations covered in the previous lessons, you’ll now be able to harness the benefits of the groundwork laid so far. Get ready to unlock the full potential of our huge integer class!
5 12 200 2 999999999999999999999999999999 -61969987109750917095790
So far we have implemented several operators such as the <
, <=
, >
, >=
, ==
, and !=
comparison operators, +
, -
, +=
, -=
, ++
, and --
arithmetic operators, and the <<
basic input output operator for printing huge integers. Now, let’s move toward more complex arithmetic operators like multiplication *
and division /
.
Multiplication and division of huge integers
Let’s say we want to multiply two numbers, and . The first and simple idea to multiply these two numbers is by addition. For example, is . In the same way, if we add to itself times, then we can get the result of .
We can implement this idea as follows:
HugeInt HugeInt::operator*(const HugeInt & b)const{HugeInt R = HugeInt::Zero();for (HugeInt a=HugeInt::One(); a <= b; ++a){R += *this;}return R;}
Line 3: The code first initializes a new
HugeInt
objectR
with a value of zero. This object will hold the result of the multiplication.Line 4: The loop initialization statement,
HugeInt a = HugeInt::One()
, creates a newHugeInt
object with a value of1
, which is used as the loop index variable. Thea <= I2
loop condition checks whether the current value ofa
is less than or equal to the value ofI2
. Thea++
loop update statement increments the value ofa
by1
after each iteration.Lines 4–7: The multiplication result is maintained using a simple repeated addition algorithm. A
for
loop is used to iterate from the1
value to the value ofI2
. At each iteration, the current object is added toR
.
Test the code above in your implementation.
Power of object-oriented design
Look at the power of the class we’ve already built. Imagine the challenges we would have faced without implementing these essential operators. Take a closer look at the multiplication algorithm, where we seamlessly utilize operators such as operator<=
, operator++
(pre-increment), operator+=
, and operator=
(which is called inside operator+=
and operator++
too). This exemplifies the true power of object-oriented programming, as it hides the intricate details and provides a clean and elegant syntax.
Our huge integer class makes complex calculations simpler and more accessible, allowing us to utilize our coding potential and achieve a truly elegant code.
Implementing operator/
and operator%
How can we perform division (operator/
) using subtraction? Write the code in your implementation above. Look at the image below explaining the process.
If you’re stuck, click the “Show Solution” button.
If you’re stuck, click the “Show Solution” button.
How can we perform the modulus (operator%
) using subtraction? Write the code above in your implementation. Refer again to the image above explaining the process.
If you’re stuck, click the “Show Solution” button.
Challenge: The serious impracticality
There is a ...