CS105: Introduction to Computer Programming: C++

Assignment #2: Defining a BigInt Class

Due

Tuesday, February 10th at 11:59pm

Overview

Now that we've explored the limits of the built-in long int and unsigned long int types, how can we do better? For this assignment, you'll define a class called BigInt that will hold positive integers up to 20 digits in length.

Requirements

Your BigInt class should meet the following requirements:

  1. The BigInt class should be defined in two files, BigInt.h and BigInt.cc. The header file should contain the description of your class, and the code file should contain the implementation of the functions. See the class notes for the third lecture for an example of how to define a class this way.
  2. In addition, you should create a main.cc file that contains a main function, so you can compile and test your BigInt class. You wouldn't write code without testing it, would you? (You don't need to turn in your main.cc.)
  3. In order to save time compiling, save this Makefile into the directory where your code is. You can use it to quickly compile your code -- just cd to the directory with your code and type make. The make program will use the Makefile to compile your three files (BigInt.cc, BigInt.h, and main.cc) into an executable called main.
  4. Your BigInt class should store its data using an integer array of size 20. Each element of the array should hold one digit of the number. For example, if the number you want to store is 658, then one element in your 20-digit array should be 6, one should be 5, and one should be 8. It's up to you to decide exactly where those elements should go, and what the other 17 elements should be.
  5. Inside your class, you should have a constructor that takes an unsigned int and creates a new BigInt out of it:
      BigInt b1( 0 );
      BigInt b2( 314159 );
      BigInt b3( 123456789 );
    
  6. You should also define a print function that displays the number stored in your class to the screen:
      BigInt b1( 0 );
      b1.print();             // should print '0' (without the quotes)
      BigInt b2( 314159 );
      b2.print();             // ...and '314159'
      BigInt b3( 123456789 ); 
      b3.print();             // ...and '123456789'
    
    You shouldn't display any leading zeros or whitespace when printing your number.

Compiling and Running

After you've saved the Makefile and created BigInt.cc, BigInt.h, and main.cc, you should be able to compile your code 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 assignment2 as the assignment name.

Submission Checklist

Easily done, you say?

Extra Credit (+20%)

Add the following public utility functions to your BigInt class: