CS105: Introduction to Computer Programming: C++

Assignment #3: On Being Correct

Due

Tuesday, February 17th at 11:59pm

Overview

For this assignment, you'll

  1. add two constructors to your BigInt class,
  2. add an isEven function to your BigInt class,
  3. and make sure all of your class functions are const correct.

Requirements

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

  1. Inside your class, you should add a constructor that takes a const string reference (const string &s, for example) and creates a new BigInt out of it:
      string s = "0";
      BigInt b1( s );
      b1.print();             // should print '0' (without quotes)
      BigInt b2( "314159" );
      b2.print();             // ...and '314159'
      BigInt b3( "123456789" ); 
      b3.print();             // ...and '123456789'
      BigInt b4( "99999999999999999999" ); 
      b4.print();             // ...and '99999999999999999999'
    
    Remember, you shouldn't display any leading zeros or whitespace when printing your number.
  2. Next up: add a copy constructor to your class. A copy constructor always takes a single argument -- a const reference to an instance of your class (const BigInt &other, for example) and creates a new BigInt that has the same value as the BigInt that is passed in. For example:
      string s = "0";
      BigInt b1( s );
      b1.print();             // should print '0' (without quotes)
      BigInt b2( b1 );
      b2.print();             // ...and '0' again
      BigInt b3( "99999999999999999999" ); 
      b3.print();             // should print '99999999999999999999'
      BigInt b4( b3 ); 
      b4.print();             // ...and '99999999999999999999' again
    
  3. Now add a little public utility function to your class called isEven. It should take no arguments, and return a boolean value that is true if the BigInt is even, false otherwise. For example:
      string s = "0";
      BigInt b1( s );
      cout << b1.isEven() << '\n';  // should print '1' (booleans display as 0 and 1)
      BigInt b2( "314159" );
      cout << b2.isEven() << '\n';  // ...not even, so we should see '0'
      BigInt b3( "42" ); 
      cout << b3.isEven() << '\n';  // ...even, so we expect '1'
    
  4. Finally, go through your class and add the const keyword where you think it's appropriate. Remember, variables can be const if they're not supposed to change, and class functions can be const if they're not supposed to change any data in the class.

    If you just can't stand using const in some place that you think it could possibly appear, justify your reasoning in a comment.

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

Submission Checklist

You've finished, and there's nothing new on the internet, so it's time for...

Extra Credit (+20%)

Add the following public utility functions to your BigInt class: