fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class String
  7. {
  8. char* a;
  9. int size;
  10. public:
  11.  
  12. String()
  13. {
  14. this->size = 80;
  15. this->a = new char[size+1];
  16. }
  17. String(const char* ptr):String()
  18. {
  19.  
  20. this->setString(ptr);
  21.  
  22. }
  23. ~String() {
  24. delete[] this->a;
  25. }
  26.  
  27. void setString(const char* ptr) {
  28. if (ptr == nullptr)
  29. {
  30. return;
  31. }
  32. this->size = strlen(ptr);
  33. delete[] this->a;
  34. this->a = new char[size + 1];
  35. for (size_t i = 0; i < size + 1; i++)
  36. {
  37. this->a[i] = ptr[i];
  38. }
  39. }
  40.  
  41.  
  42. void show() {
  43. for (size_t i = 0; i < size; i++)
  44. {
  45. cout << this->a[i];
  46. }
  47. cout << endl<<size<<endl;
  48. }
  49.  
  50. char operator[](int ind)
  51. {
  52. return a[ind];
  53. };
  54.  
  55. int operator()(const char* tmp) {
  56. for (size_t i = 0; i < size; i++)
  57. {
  58. if (a[i] == tmp[0])
  59. {
  60.  
  61. return i;
  62. }
  63. }
  64. return -1;
  65. };
  66. operator int() {
  67. return size;
  68. }
  69. void operator++(int) {
  70.  
  71. size++;
  72. a = new char[size];
  73.  
  74. }
  75. };
  76.  
  77.  
  78. int main()
  79. {
  80. String a;
  81.  
  82.  
  83. a.setString("words");
  84. cout<<a("s")<<endl;
  85. cout<<a[2]<<endl;
  86.  
  87. a.show();
  88. a++;
  89. a.show();
  90. cout << (int)a << endl;
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0s 5496KB
stdin
Standard input is empty
stdout
4
r
words
5

6
6