#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cassert>
using namespace std;
class LargeInteger {
private:
static int nextLargeIntegerId;
int id;
int numDigits; // number of digits in LargeInt / size of alloc array
int* digits; // the digits of the LargeInt we are representing
public:
LargeInteger(int value);
~LargeInteger();
string tostring();
};
LargeInteger::LargeInteger(int value)
{
// set this instance id
id = nextLargeIntegerId++;
numDigits = (int)log10((double)value) + 1;
// allocate an array of the right size
digits = new int[numDigits];
// iterate through the digits in value, putting them into our
// array of digits.
int digit;
for (int digitIndex = 0; digitIndex < numDigits; digitIndex++) {
// least significant digit
digit = value % 10;
digits[digitIndex] = digit;
// integer division to chop of least significant digit
value = value / 10;
}
}
LargeInteger::~LargeInteger()
{
cout << " destructor entered, freeing my digits" << endl
<< " id = " << id << endl
//<< " value=" << tostring() // uncomment this after you implement tostring()
<< endl;
delete[] this->digits;
}
string LargeInteger::tostring()
{
string intValue;
for (int i=numDigits-1; i>=0; i--)
intValue += digits[i] + '0';
return intValue;
}
int LargeInteger::nextLargeIntegerId = 0;
int main() {
// test constructors, destructors and tostring()
cout << "Testing Constructors, tostring() and destructor:" << endl;
cout << "-------------------------------------------------------------" << endl;
LargeInteger li1(3483);
cout << "li1 = " << li1.tostring() << endl;
return 0;
}