Assignment 2 - Big Ints
Due: February 22
To gain experience using pointers, dynamic memory allocation and operator overloading.
In C++, integers have size limits, which are dependent on the machine and compiler.
On cinnamon, an integer is 4 bytes.
The biggest integer type, long long, is 8 bytes or 64 bits.
This type can hold values up to 2^64 - 1, or 18446744073709551615.
If we want to store values larger than this, we can't use any built in type to do it.
Your task for this assignment is to write a class, called BigInt that can store non-negative numbers of any size, from 0 all the way up without limit (although, at some point, a computer can run out of memory).
Your class will store the digits of the number in a dynamic array, and provide a number of operators to allow users of the class to use it like a built-in int.
The use of this class is demonstrated in this main program which you can use to test your BigInt class.
For this program, you aren't writing your own main function, you can simply use the one provided.
- The BigInt class should store the number as a dynamic array of digits.
Your class must allocate and de-allocate memory correctly, without memory leaks or errors.
You also should not allocate more memory than is necessary.
- Your class should have a default constructor (one with no parameters) that sets the BigInt to the value of 0.
- There must be another constructor that takes an unsigned int as a parameter and sets the BigInt to the value of the parameter.
- A third constructor must take a C-String as a parameter and set the BigInt to the value of the string (you can assume that the string contains only digits). This will allow us to create BigInts that won't fit in standard integers such as:
- Your class should have a copy constructor, destructor and assignment operator.
- In order to print BigInt objects, you must overload the output operator <<.
This should print the number itself as if it were an int. Don't output any extra text.
- Likewise, your class should overload the input operator >> to allow for inputting BigInt objects.
To do this, you can assume the user will enter fewer than 100 digits and will not enter any non-digits.
Your operator should initially read in the number as a string and convert it into the BigInt.
- You should overload each of the six comparison operators (<, <=, >, >=, ==, !=).
Each of these should return a boolean value, and use the usual meaning of comparison for numbers.
- You must overload the addition operator, to add two BigInt objects. The operator should return a BigInt object for the sum.
You should use the same algorithm that you use to add numbers using pencil and paper - and remember to carry!
- All BigInts will be non-negative!
- Start this program early!
- You can store your digits as integers or characters.
For example the digit 7 can be stored as an actual 7, or as '7' (ASCII code 55).
Using integers will make the addition easier, using characters will make I/O easier.
- You can make the digit in location 0 the most significant digit or the least significant digit.
Some functions will be easier one way and others the other way. Be sure to be consistent!
- I would suggest writing the functions in the following order:
- The constructors.
- The output operator.
- The copy constructor, assignment operator and destructor.
- The comparison operators.
- The addition operator.
- The input operator.
- Test each function after writing it.
For extra credit, also provide an overload of the multiplication operator to multiply two BigInt objects and return the product as a BigInt object.
- No global variables other than constants.
- All member data of a class must be private.
- Your source code should be readable and reasonably indented.
- You must provide comments in your code.
- Member functions that don't change a classes member data should be marked as const.
- Your class should not cause memory leaks or segfaults in any situation.
At the top of your program, please include a comment that includes your name, the date and the assignment number.
Then submit your program by emailing your code to firstname.lastname@example.org.
Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.