CS105: Introduction to Computer Programming: C++

Assignment #5: Hello, Operator?

Due

Tuesday, March 3rd at 11:59pm

Overview

Overload four operators for your BigInt class: ++ (both pre and post), <, and ==.

Requirements

In addition to meeting the requirements from the previous assignments, your BigInt class should also have the following overloaded operators:

  1. The ++ pre-increment operator should increment the value of the BigInt by one. The operator should evaluate to the new, incremented BigInt. For example:
      BigInt b1( "1" );
      (++b1).print();        // should display '2'
      b1.print();            // should display '2'
    
    If incrementing the BigInt would result in an overflow (i.e. a number greater than 20 digits), the BigInt should be set to zero.
  2. The ++ post-increment operator should also increment the value of the BigInt by one. However, the post-increment operator should evaluate to the original, un-incremented BigInt. For example:
      BigInt b1( "1" );
      (b1++).print();        // should display '1'
      b1.print();            // should display '2'
    
    As before, if incrementing the BigInt would result in an overflow (i.e. a number greater than 20 digits), the BigInt should be set to zero.
  3. The less-than comparison operator < should be implemented as a global friend function of the BigInt class. This function should take two BigInt arguments -- the left and right-hand side of the operator -- and should return true if the left BigInt is less than the right one. For example:
      BigInt b1( "314159" );
      BigInt b2( "1" );
      cout << (b1 < b2) << '\n';   // should display 0
      cout << (b2 < b1) << '\n';   // should display 1
    
  4. The equal-to comparison operator == should be implemented as a global friend function of the BigInt class. This function should take two BigInt arguments -- the left and right-hand side of the operator -- and should return true if the two BigInt objects are equal. For example:
      BigInt b1( "314159" );
      BigInt b2( "1" );
      cout << (b1 == b2) << '\n';   // should display 0
      BigInt b3( "42" );
      BigInt b4( "42" );
      cout << (b3 == b4) << '\n';   // should display 1
    

Compiling and Running

As usual, you should be able to compile your code using the Makefile from the last assignment by typing:

make

If all goes well, you shouldn't see any error messages. Then you can run your code by typing:

./main

Whatever you've included in your main function should run, and any output that is generated should appear on your screen.

Turning it in

When you're happy with your code, use the turnin program to submit your BigInt.cc and BigInt.h files. Use assignment5 as the assignment name.

Submission Checklist

When you just can't get enough, there's...

Extra Credit (+20%)