35657

Untitled

Jun 2nd, 2023
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. class String {
  11.  
  12. public:
  13.  
  14. String() : size(0), capacity(15), str(new char[15]) {
  15. total_number_strings++;
  16. }
  17.  
  18. String(const int string_capacity) : size(0), capacity(string_capacity), str(new char[string_capacity]) {
  19. total_number_strings++;
  20. }
  21.  
  22. String(const char new_str[]) {
  23. capacity = strlen(new_str) + 1;
  24. str = new char[capacity];
  25. strcpy(str, new_str);
  26. size = capacity - 1;
  27. total_number_strings++;
  28. }
  29.  
  30. String(const String& other) : size(other.size), capacity(other.capacity), str(new char[capacity]) {
  31. strcpy(str, other.str);
  32. total_number_strings++;
  33. }
  34.  
  35. String& operator=(const String& other) {
  36. size = other.size;
  37. capacity = other.capacity;
  38. delete[] str;
  39. str = new char[capacity];
  40. strcpy(str, other.str);
  41. return *this;
  42. } // перегрузка оператора присваивания =
  43.  
  44. char& operator[] (const int index) {
  45. if (index < 0 || index >= size) {
  46. cout << "Invalid index" << endl;
  47. abort;
  48. }
  49. return str[index];
  50. } // перегрузка оператора индексирования (сеттер)
  51.  
  52. char operator[] (const int index) const {
  53. if (index < 0 || index >= size) {
  54. cout << "Invalid index" << endl;
  55. abort;
  56. }
  57. return str[index];
  58. } // перегрузка оператора индексирования для константных объектов
  59.  
  60. bool operator==(const String& right) const {
  61. if (right.size != size) {
  62. return false;
  63. }
  64. for (int i = 0; i < size; i++) {
  65. if (str[i] != right.str[i])
  66. return false;
  67. }
  68. return true;
  69. }
  70.  
  71. bool operator!=(const String& right) const {
  72. return !(*this == right);
  73. }
  74.  
  75. int Size() const {
  76. return size;
  77. }
  78.  
  79. int Capacity() const {
  80. return capacity;
  81. }
  82.  
  83. static int GetTotalNumberStrings() {
  84. return total_number_strings;
  85. }
  86.  
  87. ~String() {
  88. delete[] str;
  89. total_number_strings--;
  90. }
  91.  
  92. private:
  93. int size;
  94. int capacity;
  95. char* str;
  96. static int total_number_strings;
  97. };
  98.  
  99. ostream& operator<<(ostream& output, const String& string) {
  100. for (int i = 0; i < string.Size(); i++) {
  101. output << string[i];
  102. }
  103. return output;
  104. }
  105.  
  106. //istream& operator>>(istream& input, String& string) {
  107. //
  108. //}
  109.  
  110.  
  111. int String::total_number_strings = 0;
  112.  
  113.  
  114. int main() {
  115. String my_string;
  116. cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  117. my_string = "Hello";
  118.  
  119. cout << my_string << endl;
  120.  
  121. cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  122. my_string = "Hello my dear friend";
  123. cout << my_string << endl;
  124. cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  125. my_string[2] = 'm';
  126. cout << my_string << endl;
  127. cout << my_string[1] << endl;
  128. String my_string2("1234");
  129. cout << my_string2 << endl;
  130. cout << "Size: " << my_string2.Size() << " Capacity: " << my_string2.Capacity() << endl;
  131. const String my_string3(my_string);
  132. cout << my_string3[1] << endl;
  133. cout << my_string3<< endl;
  134. cout << String::GetTotalNumberStrings() << endl;
  135. cout << (my_string == my_string2) << endl;
  136. cout << (my_string != my_string2) << endl;
  137. String my_string4;
  138. my_string4 = my_string;
  139. cout << my_string4 << endl;
  140. my_string4 = "Hello";
  141. cout << my_string4 << endl;
  142.  
  143. string str;
  144. cin >> str;
  145. cout << str << endl;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment