fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cmath>
  5. #include <cassert>
  6. #include <iterator>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. class LargeInteger {
  12. private:
  13. static int nextLargeIntegerId;
  14. int id;
  15. string digits; // the digits of the LargeInt we are representing
  16.  
  17. public:
  18. LargeInteger(int value);
  19. ~LargeInteger();
  20. string tostring();
  21. };
  22.  
  23. LargeInteger::LargeInteger(int value)
  24. {
  25. // set this instance id
  26. id = nextLargeIntegerId++;
  27.  
  28. // iterate through the digits in value, putting them into our
  29. // array of digits.
  30. unsigned char digit;
  31. while(value) {
  32. // least significant digit
  33. digit = value % 10;
  34. digits += digit;
  35.  
  36. // integer division to chop of least significant digit
  37. value = value / 10;
  38. }
  39. }
  40.  
  41. LargeInteger::~LargeInteger()
  42. {
  43. cout << " destructor entered, freeing my digits" << endl
  44. << " id = " << id << endl
  45. //<< " value=" << tostring() // uncomment this after you implement tostring()
  46. << endl;
  47. }
  48.  
  49. string LargeInteger::tostring()
  50. {
  51. string intValue(digits.size(),' ');
  52.  
  53. transform (digits.rbegin(), digits.rend(), intValue.begin(), [](auto &x){return x+'0'; }) ;
  54. return intValue;
  55. }
  56. int LargeInteger::nextLargeIntegerId = 0;
  57.  
  58. int main() {
  59. // test constructors, destructors and tostring()
  60. cout << "Testing Constructors, tostring() and destructor:" << endl;
  61. cout << "-------------------------------------------------------------" << endl;
  62. LargeInteger li1(3483);
  63. cout << "li1 = " << li1.tostring() << endl;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Testing Constructors, tostring() and destructor:
-------------------------------------------------------------
li1 = 3483
 destructor entered, freeing my digits
 id = 0