Constructors, Load, and Print Functions
Learn how to load and print huge integers.
We'll cover the following...
This lesson will cover the basics of loading and printing huge integers in C++, which can consist of several hundred digits. We’ll explore two different methods of storing these numbers and assess which is a better approach.
Your implementation
Review each file carefully to understand the task requirements before starting to code.
5 100 200 78974759690493727283939222737475969049372728393922332957273757217 +3329593827228272737572172737475969049372728393922 -300
Note: Write the code yourself by following the instructions below.
Huge integer ADT
Let’s create an ADT with the HugeInt
class with the following attributes and methods:
// Define HugeInt.hclass HugeInt{int size; // Size of digitsint* Ds; // Digit array will store the digitsbool isNeg; // Numbers can be negative or positivepublic:HugeInt(string s); // Method to assign a huge integerfriend ostream& operator<<(ostream&, const HugeInt& I); // Print an integers on the console};// Define main.cppint main(){HugeInt I("234");// Should call the 2nd constructor on line 9 (once we implement their definitions in HugeInt.cpp)cout << "I: " << I << endl;}
The following are the member attributes:
Line 4: The
size
variable holds the count of digits in each huge integer.Line 5: The
Ds
integer pointer points to a heap array that stores each digit of the huge integer separately.Line 6: The boolean variable
isNeg
is set totrue
if the huge integer is negative andfalse
otherwise.
The following are the member functions:
Line 8: This constructor is invoked when we want to load a huge integer from a file.
Line 17: This constructor is invoked when we want to initialize the huge integer with a string.
Line 18: The
operator<<
constructor is invoked, which is thefriend
function that needs access to the private attributes of huge integers to display on the console.
Note: Add the class declaration part in
HugeInt.h
.
Note: Once we implement the definition of
HugeInt
ADT in theHugeInt.cpp
file (which we’ll discuss shortly), the providedmain()
function in lines 15–20 should execute properly.
Let’s proceed to the next section, where we’ll define all the member functions of the huge integer ADT.
Implementing HugeInt.cpp
Before implementing our first constructor, which requires reading from the HIs.txt
file, let’s review what is to be loaded:
510020078927374759690493727283939222737475969049372728393922332957273757217+3329593827228272737572172737475969049372728393922-300
Line 1: Here,
5
denotes the total count of huge integers to be loaded.Line 2–6: Each individual huge integer is listed on a separate line, i.e.,
100
on the first line, followed by200
on the next line, and so on.
Using getline()
and operator>>
with ifstream
and string
While loading five huge integers into an integer array (from the sample HIs.txt
file above), we encounter the challenge of determining the size of each integer. We can overcome this challenge using two ways:
- If we know the maximum number of digits an integer can have, let’s say 1,000, we can utilize
Rdr.getline(s, 1000)
to load the integer array. This method allows us to read the integer array as a string, considering the maximum length of the integer as an argument.
string s; ifstream Rdr("HIs.txt");
Rdr.getline(s,1000); // The maximum # of digits could be 1000
s.size(); // This holds the actual size of the loaded huge integer
- However, if the size of each integer is unknown or even more than a thousand digits, we need a different approach. In this case, we can leverage
operator>>
overloaded withifstream
andstring
parameters from the class. By reading character by character until a delimiter, such as a space or end-of-line character, is encountered, this operator allows us to load each integer as a dynamically expanding string. We can then determine the exact size of each integer using thesize()
member function of the string, enabling us to allocate the appropriate dynamic array based on the number of digits for each huge integer.
string s; ifstream Rdr("HIs.txt");
...