fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. class MyString
  7. {
  8. private:
  9. char *m_data;
  10. int m_length;
  11.  
  12. public:
  13. MyString(const char *source="")
  14. {
  15. assert(source); // make sure source isn't a null string
  16.  
  17. // Find the length of the string
  18. // Plus one character for a terminator
  19. m_length = std::strlen(source) + 1;
  20.  
  21. // Allocate a buffer equal to this length
  22. m_data = new char[m_length];
  23.  
  24. // Copy the parameter string into our internal buffer
  25. for (int i{ 0 }; i < m_length; ++i)
  26. m_data[i] = source[i];
  27.  
  28. // Make sure the string is terminated
  29. m_data[m_length-1] = '\0';
  30. }
  31.  
  32. ~MyString() // destructor
  33. {
  34. // We need to deallocate our string
  35. delete[] m_data;
  36. }
  37.  
  38. char* getString() { return m_data; }
  39. int getLength() { return m_length; }
  40. };
  41.  
  42. int main() {
  43. // your code goes here
  44. MyString hello("Hello, world!");
  45. return 0;
  46. }
Success #stdin #stdout 0s 4248KB
stdin
Standard input is empty
stdout
Standard output is empty