using tostring within a class.
#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>
using namespace std;
Here is my class LargeInteger. Everything is correct in here Im quite sure.
class LargeInteger {
private:
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();
};
Constructor for class LargeInteger with a parameter int value.
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;
}
}
Destructor
LargeInteger::~LargeInteger()
{
cout << " destructor entered, freeing my digits" << endl
<< " id = " << id << endl
//<< " value=" << tostring() // uncomment this after you implement tostring()
<< endl;
delete[] this->digits;
}
This is where I'm confused. What do I put here? I tried this but I dont know what to set intValue to in order to get the value I want.
string LargeInteger::tostring()
{
string intValue;
//intValue = ??
return intValue;
}
Main function
int main(int argc, char** argv)
{
// test constructors, destructors and tostring()
cout << "Testing Constructors, tostring() and destructor:" << endl;
cout << "-------------------------------------------------------------" << endl;
LargeInteger li1(3483);
cout << "li1 = " << li1.tostring() << endl;
return 0
}