Guest User

Untitled

a guest
Apr 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. class Str
  2. {
  3. public:
  4. Str();
  5. Str(int n);
  6. Str(char str[]);
  7. ~Str();
  8. void output();
  9.  
  10. private:
  11. char* _str;
  12. int _n;
  13. };
  14.  
  15. Str::Str()
  16. {
  17. _n = 0;
  18. _str = new char[_n];
  19. }
  20. Str::Str(int n)
  21. {
  22. _n = n;
  23. _str = new char[_n];
  24. }
  25.  
  26. Str::Str(char str[])
  27. {
  28. int _n = strlen(str);
  29. _str = new char[_n];
  30. //strcpy(_str, str);
  31. for (int i = 0; i < _n; i ++)
  32. {
  33. _str[i] = str[i];
  34. }
  35. }
  36.  
  37. void Str::output()
  38. {
  39. std::cout << _str << std::endl;
  40. }
  41. Str::~Str()
  42. {
  43. delete[] _str;
  44. }
  45. int main()
  46. {
  47. Str e;
  48. e.output();
  49. Str a;
  50. Str b(1);
  51. Str c("123");
  52. b.output();
  53. c.output();
  54. }
  55.  
  56. Str::Str(const char * str)
  57. {
  58. _n = strlen(str) + 1;
  59.  
  60. _n = 0;
  61. _str = new char[_n];
  62.  
  63. #include <iostream>
  64. #include <iomanip>
  65.  
  66. using namespace std;
  67.  
  68.  
  69. class Str
  70. {
  71. public:
  72. Str(size_t n = 1);
  73. Str(const char *str);
  74. ~Str();
  75. void output();
  76.  
  77. private:
  78. char* _str;
  79. size_t _n;
  80. };
  81.  
  82. Str::Str(size_t n)
  83. {
  84. _n = n;
  85. *(_str = new char[_n]) = 0;
  86. }
  87.  
  88. Str::Str(const char *str)
  89. {
  90. _n = strlen(str)+1;
  91. _str = new char[_n];
  92. strcpy(_str, str);
  93. }
  94.  
  95. void Str::output()
  96. {
  97. std::cout << _str << std::endl;
  98. }
  99.  
  100. Str::~Str()
  101. {
  102. delete[] _str;
  103. }
  104.  
  105. int main()
  106. {
  107. Str e;
  108. e.output();
  109. Str a;
  110. Str b(1);
  111. Str c("123");
  112. b.output();
  113. c.output();
  114. }
  115.  
  116. Str::Str()
  117. {
  118. _n = 0;
  119. _str = NULL;
  120. }
  121.  
  122. Str::Str(int n)
  123. {
  124. _n = n;
  125. _str = new char[_n + 1];
  126. _str[0] = '';
  127. }
  128.  
  129. Str::Str(char str[])
  130. {
  131. _n = strlen(str);
  132. _str = new char[_n + 1];
  133. ...
  134. _str[_n] = '';
  135. }
Add Comment
Please, Sign In to add comment