CS105: Introduction to Computer Programming: C++

Assignment #6: Adding (and Displaying)


Tuesday, March 10th at 11:59pm


Add an overloaded addition operator to your BigInt class. Also, put an overloaded display operator in there.


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

  1. You've already written a print method, but that's kind of awkward. Leave your print method there, but add an overloaded stream insertion operator (<<) to your class.

    Overloading a stream insertion operator for any class always follows the same pattern:

    1. It should be implemented as a global friend function of your class called operator<<.
    2. It should take two arguments: a reference to an ostream (e.g. ostream& os) and a const reference to an instance of your class (e.g. const BigInt &rhs).
    3. The job of the function is to display rhs to the stream os.
    4. It should return a reference to the ostream that is passed in, so you can chain multiple stream insertions together.

    For example:

      BigInt b1( "42" );
      cout << b1 << endl;   // should display 42 to standard output
  2. Add an addition operator to your class.

    The addition 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 a BigInt that represents the sum of the left and the right arguments.

    For example:

      BigInt b1( "3" );
      BigInt sum1 = 1 + b1;
      cout << sum1 << endl;            // should display 4
      BigInt b2( "9" );
      BigInt b3( "1" );
      BigInt sum2 = b2 + b3;
      cout << sum2 << endl;            // should display 10
      BigInt b4( "1999" );
      BigInt sum3 = b4 + 3;
      cout << sum3 << endl;            // should display 2002
      BigInt b5( "9999999999" );
      BigInt b6( "1" );
      cout << (b5+b6) << endl;         // should display 10000000000

    You do not have to worry about overflow beyond 20 digits, i.e. you can assume that the sum of the two numbers will be at most 20 digits long.

Compiling and Running

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


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


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 assignment6 as the assignment name.

Submission Checklist

It's chewy on the inside but crunchy on the outside!

Extra Credit (+20%)